package com.fjhx.stock.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.Condition;
import com.fjhx.entity.plan.PlanConfig;
import com.fjhx.entity.stock.StockBackDetails;
import com.fjhx.params.stock.StockBackDetailsVo;
import com.fjhx.plan.service.PlanConfigService;
import com.fjhx.stock.mapper.StockBackDetailsMapper;
import com.fjhx.stock.service.StockBackDetailsService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-08-31
 */
@Service
public class StockBackDetailsServiceImpl extends ServiceImpl<StockBackDetailsMapper, StockBackDetails> implements StockBackDetailsService {

    @Autowired
    private PlanConfigService planConfigService;

    @Override
    public Page<StockBackDetails> getPage(Map<String, String> condition) {

        QueryWrapper<StockBackDetails> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .createTimeDesc();

        Page<StockBackDetails> page = page(condition, wrapper);
        return page;
    }

    @Override
    public void add(StockBackDetailsVo stockBackDetailsVo) {
        save(stockBackDetailsVo);
    }

    @Override
    public void edit(StockBackDetailsVo stockBackDetailsVo) {
        updateById(stockBackDetailsVo);
    }

    @Override
    public void delete(StockBackDetailsVo stockBackDetailsVo) {
        removeById(stockBackDetailsVo.getId());
    }

    @Override
    public List<Map<String, Object>> picking(Condition condition) {

        // 开始时间
        Date beginTime = condition.getBeginTime();
        // 结束时间
        Date endTime = condition.getEndTime();

        Assert.notEmpty(beginTime, "开始时间不能为空");
        Assert.notEmpty(endTime, "结束时间不能为空");

        // 人员
        String userId = condition.getStr("userId");
        // 物料名称
        String materialName = condition.getStr("materialName");
        // 工艺
        String tech = condition.getStr("tech");

        Map<String, PlanConfig> configMap = planConfigService.list().stream()
                .collect(Collectors.toMap(PlanConfig::getCode, item -> item));

        QueryWrapper<Object> wrapper = Wrappers.query()
                .between("swd.statistics_date", beginTime, endTime)
                .in("swd.StockChangeType", 20, 23)
                .func(q -> {
                    // 用户id不为空
                    if (ObjectUtil.isNotEmpty(userId)) {
                        q.eq("uu.ID", userId);
                    }
                    // 用户id为空，查询指定部门全部人员
                    else {
                        List<String> deptIdList = new ArrayList<>();

                        deptIdList.addAll(Arrays.asList(configMap.get("issue").getDeptIdSet().split(",")));
                        deptIdList.addAll(Arrays.asList(configMap.get("issueA008").getDeptIdSet().split(",")));

                        q.in("uu.DepartmentID", deptIdList);
                    }
                })
                // 物料名称
                .and(ObjectUtil.isNotEmpty(materialName), q ->
                        q.eq("m.Name", materialName).or().likeRight("m.Name", materialName + "-"))
                .notIn("m.CategoryCode", Arrays.asList(configMap.get("issue").getNotCategoryCodeSet().split(",")))
                .eq("uu.IsDelete", 0)
                .isNotNull("uu.JobNo");

        return baseMapper.getPickingList(wrapper, tech, beginTime, endTime);
    }

    @Override
    public List<Map<String, Object>> returnMaterial(Condition condition) {
        // 开始时间
        Date beginTime = condition.getBeginTime();
        // 结束时间
        Date endTime = condition.getEndTime();

        Assert.notEmpty(beginTime, "开始时间不能为空");
        Assert.notEmpty(endTime, "结束时间不能为空");

        // 人员
        String userId = condition.getStr("userId");
        // 物料名称
        String materialName = condition.getStr("materialName");
        // 工艺
        String tech = condition.getStr("tech");

        Map<String, PlanConfig> configMap = planConfigService.list().stream()
                .collect(Collectors.toMap(PlanConfig::getCode, item -> item));

        QueryWrapper<Object> wrapper = Wrappers.query()
                .between("sb.statistics_date", beginTime, endTime)
                .eq("uu.IsDelete", 0)
                .func(q -> {
                    // 用户id不为空
                    if (ObjectUtil.isNotEmpty(userId)) {
                        q.eq("uu.ID", userId);
                    }
                    // 用户id为空，查询指定部门全部人员
                    else {
                        List<String> deptIdList = new ArrayList<>();
                        deptIdList.addAll(Arrays.asList(configMap.get("returnWarehouse").getDeptIdSet().split(",")));
                        deptIdList.addAll(Arrays.asList(configMap.get("returnWarehouseA008").getDeptIdSet().split(",")));
                        q.in("uu.DepartmentID", deptIdList);
                    }
                })
                // 物料名称
                .and(ObjectUtil.isNotEmpty(materialName), q ->
                        q.eq("m.Name", materialName).or().likeRight("m.Name", materialName + "-"))
                .notIn("m.CategoryCode", Arrays.asList(configMap.get("issue").getNotCategoryCodeSet().split(",")))
                .isNotNull("uu.JobNo");

        return baseMapper.getReturnMaterialList(wrapper, tech, beginTime, endTime);
    }

    @Override
    public List<Map<String, Object>> correct(Condition condition) {
        // 开始时间
        Date beginTime = condition.getBeginTime();
        // 结束时间
        Date endTime = condition.getEndTime();

        Assert.notEmpty(beginTime, "开始时间不能为空");
        Assert.notEmpty(endTime, "结束时间不能为空");

        // 人员
        String userId = condition.getStr("userId");
        // 物料名称
        String materialName = condition.getStr("materialName");
        // 工艺
        String tech = condition.getStr("tech");

        Map<String, PlanConfig> configMap = planConfigService.list().stream()
                .collect(Collectors.toMap(PlanConfig::getCode, item -> item));

        QueryWrapper<Object> wrapper = Wrappers.query()
                .between("sc.correct_date", beginTime, endTime)
                .func(q -> {
                    // 用户id不为空
                    if (ObjectUtil.isNotEmpty(userId)) {
                        q.eq("uu.ID", userId);
                    }
                    // 用户id为空，查询指定部门全部人员
                    else {
                        List<String> deptIdList = new ArrayList<>();
                        deptIdList.addAll(Arrays.asList(configMap.get("returnWarehouse").getDeptIdSet().split(",")));
                        deptIdList.addAll(Arrays.asList(configMap.get("returnWarehouseA008").getDeptIdSet().split(",")));
                        q.in("uu.DepartmentID", deptIdList);
                    }
                })
                // 物料名称
                .and(ObjectUtil.isNotEmpty(materialName), q ->
                        q.eq("m.Name", materialName).or().likeRight("m.Name", materialName + "-"))
                .notIn("m.CategoryCode", Arrays.asList(configMap.get("headTailCorrect").getNotCategoryCodeSet().split(",")));

        return baseMapper.getCorrectList(wrapper, tech, beginTime, endTime);
    }

}
