package com.njzscloud.supervisory.route.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.route.mapper.OrderRouteMapper;
import com.njzscloud.supervisory.route.pojo.OrderRouteDetailEntity;
import com.njzscloud.supervisory.route.pojo.OrderRouteEntity;
import com.njzscloud.supervisory.route.service.OrderRouteDetailService;
import com.njzscloud.supervisory.route.service.OrderRouteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单路线管理
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderRouteServiceImpl extends ServiceImpl<OrderRouteMapper, OrderRouteEntity> implements OrderRouteService {

    private final OrderRouteDetailService orderRouteDetailService;

    private final OrderRouteMapper orderRouteMapper;

    /**
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(OrderRouteEntity orderRouteEntity) {
        this.save(orderRouteEntity);
        // 保存详情
        List<OrderRouteDetailEntity> detailEntityList = orderRouteEntity.getDetailEntityList();
        if (null != detailEntityList && detailEntityList.size() > 0) {
            for (OrderRouteDetailEntity detailEntity : detailEntityList) {
                detailEntity.setOrderRouteId(orderRouteEntity.getId());
            }
            orderRouteDetailService.saveBatch(detailEntityList);
        }
    }

    /**
     * 修改
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(OrderRouteEntity orderRouteEntity) {
        this.updateById(orderRouteEntity);
        // 删除原有数据，然后直接新增
        orderRouteDetailService.remove(Wrappers.<OrderRouteDetailEntity>lambdaQuery()
                .in(OrderRouteDetailEntity::getOrderRouteId, orderRouteEntity.getId()));
        List<OrderRouteDetailEntity> detailEntityList = orderRouteEntity.getDetailEntityList();
        if (null != detailEntityList && detailEntityList.size() > 0) {
            for (OrderRouteDetailEntity detailEntity : detailEntityList) {
                detailEntity.setOrderRouteId(orderRouteEntity.getId());
            }
            orderRouteDetailService.saveBatch(detailEntityList);
        }
    }

    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
        for (Long id : ids) {
            // 删除详细路线
            orderRouteDetailService.remove(Wrappers.<OrderRouteDetailEntity>lambdaQuery()
                    .in(OrderRouteDetailEntity::getOrderRouteId, id));
        }
    }

    /**
     * 详情
     */
    @Override
    public OrderRouteEntity detail(Long id) {
        OrderRouteEntity entity = orderRouteMapper.selectDetailById(id);
        List<OrderRouteDetailEntity> detailEntityList = orderRouteDetailService.list(Wrappers.lambdaQuery(OrderRouteDetailEntity.class)
                .eq(OrderRouteDetailEntity::getOrderRouteId, entity.getId())
                .orderByAsc(OrderRouteDetailEntity::getSort));
        if (null != detailEntityList && detailEntityList.size() > 0) {
            entity.setDetailEntityList(detailEntityList);
        }
        return entity;
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult<OrderRouteEntity> paging(PageParam pageParam, OrderRouteEntity routeEntity) {
        QueryWrapper<OrderRouteEntity> ew = Wrappers.<OrderRouteEntity>query()
                .eq("a.deleted", 0)
                .like(StrUtil.isNotBlank(routeEntity.getName()), "a.name", routeEntity.getName())
                .eq(null != routeEntity.getCompanyId(), "a.company_id", routeEntity.getCompanyId())
                .eq(null != routeEntity.getStationId(), "a.station_id", routeEntity.getStationId())
                .eq(null != routeEntity.getOrderId(), "a.order_id", routeEntity.getOrderId())
                .eq(null != routeEntity.getProjectId(), "a.project_id", routeEntity.getProjectId());
        IPage<OrderRouteEntity> page = orderRouteMapper.paging(pageParam.toPage(), ew);
        for (OrderRouteEntity entity : page.getRecords()) {
            List<OrderRouteDetailEntity> detailEntityList = orderRouteDetailService.list(Wrappers.lambdaQuery(OrderRouteDetailEntity.class)
                    .eq(OrderRouteDetailEntity::getOrderRouteId, entity.getId()).orderByAsc(OrderRouteDetailEntity::getSort));
            if (null != detailEntityList && detailEntityList.size() > 0) {
                entity.setDetailEntityList(detailEntityList);
            }
        }
        return PageResult.of(page);
    }

    /**
     * 根据订单Id查看路线详情
     */
    @Override
    public OrderRouteEntity getByOrderId(Long orderId) {
        OrderRouteEntity entity = orderRouteMapper.getByOrderId(orderId);
        if (null != entity) {
            List<OrderRouteDetailEntity> detailEntityList = orderRouteDetailService.list(Wrappers.lambdaQuery(OrderRouteDetailEntity.class)
                    .eq(OrderRouteDetailEntity::getOrderRouteId, entity.getId())
                    .orderByAsc(OrderRouteDetailEntity::getSort));
            if (null != detailEntityList && detailEntityList.size() > 0) {
                entity.setDetailEntityList(detailEntityList);
            }
        } else {
            throw Exceptions.error("未查询到路线");
        }
        return entity;
    }

}

