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.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.order.pojo.entity.OrderExpenseItemsEntity;
import com.njzscloud.supervisory.route.mapper.RouteMapper;
import com.njzscloud.supervisory.route.pojo.RouteDetailEntity;
import com.njzscloud.supervisory.route.pojo.RouteEntity;
import com.njzscloud.supervisory.route.service.RouteDetailService;
import com.njzscloud.supervisory.route.service.RouteService;
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 RouteServiceImpl extends ServiceImpl<RouteMapper, RouteEntity> implements RouteService {

    private final RouteDetailService routeDetailService;

    private final RouteMapper routeMapper;

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

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

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

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

    /**
     * 分页查询
     */
    @Override
    public PageResult<RouteEntity> paging(PageParam pageParam, RouteEntity routeEntity) {
        QueryWrapper<RouteEntity> ew = Wrappers.<RouteEntity>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.getProjectId(), "a.project_id", routeEntity.getProjectId());
        IPage<RouteEntity> page = routeMapper.paging(pageParam.toPage(), ew);
        for (RouteEntity entity : page.getRecords()) {
            List<RouteDetailEntity> detailEntityList = routeDetailService.list(Wrappers.lambdaQuery(RouteDetailEntity.class)
                    .eq(RouteDetailEntity::getRouteId, entity.getId()).orderByAsc(RouteDetailEntity::getSort));
            if (null != detailEntityList && detailEntityList.size() > 0) {
                entity.setDetailEntityList(detailEntityList);
            }
        }
        return PageResult.of(page);
    }

}

