package com.fjhx.applet.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.applet.mapper.MaterialReceiveMapper;
import com.fjhx.applet.service.MaterialReceiveService;
import com.fjhx.base.BaseEntity;
import com.fjhx.utils.Assert;
import com.fjhx.utils.WxAppletUtil;
import org.springblade.system.user.feign.IUserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MaterialReceiveServiceImpl extends ServiceImpl<MaterialReceiveMapper, BaseEntity> implements MaterialReceiveService {


    @Autowired
    private IUserClient userClient;

    @Override
    public synchronized void submit(String checkUserId) {

        List<Map<String, Object>> list = baseMapper.selectWaterDetail();

        Map<Integer, List<Map<String, Object>>> stockChangeType =
                list.stream().collect(Collectors.groupingBy(item -> (Integer) item.get("stockChangeType")));

        // 补出库处理
        List<Map<String, Object>> outList = stockChangeType.get(29);
        if (outList != null) {

            // 流程关联id
            String flowLinkId = list.get(0).get("id").toString();

            // 发起审批流
            baseMapper.insertFlowApplyCheck(
                    IdWorker.getIdStr(),
                    "202208240000000000000000001",
                    "20220824000000000000000000101",
                    null,
                    "20220824000000000000000000102",
                    checkUserId,
                    "0",
                    null,
                    flowLinkId,
                    "物料接收"
            );

            // 物料接收待审批
            List<String> idList = outList.stream().map(item -> item.get("id").toString()).collect(Collectors.toList());
            baseMapper.updateReceive(idList, 0, 23, 1, flowLinkId);
        }

        // 出库处理
        List<Map<String, Object>> repairList = stockChangeType.get(30);
        if (repairList != null) {

            // 获取openId
            List<String> accountList = repairList.stream()
                    .map(item -> item.get("account").toString()).distinct().collect(Collectors.toList());
            new Thread(() -> {
                Map<String, String> accountMap = userClient.getOpenidByAccount(accountList, "849380");
                // 发送小程序推送
                for (String account : accountList) {
                    String openId = accountMap.get(account);
                    if (ObjectUtil.isNotEmpty(openId)) {
                        Integer num = baseMapper.countNum(account);
                        WxAppletUtil.UniformSendEntity uniformSendEntity = new WxAppletUtil.UniformSendEntity();
                        uniformSendEntity.setTouser(openId);
                        uniformSendEntity.setTemplate_id("YQNpUJwn7kdApbkO142eIeBq4qiYRfmNlVhSg_5ZdYg");
                        uniformSendEntity.setPage("pages/user/login");
                        uniformSendEntity.addData("thing3", "智能仓储");
                        uniformSendEntity.addData("thing2", num.toString());
                        uniformSendEntity.addData("thing6", DateUtil.format(new Date(), "yyyy年MM月dd日 HH:mm"));
                        WxAppletUtil.sendMessage(uniformSendEntity);
                    }
                }
            }).start();

            List<String> idList = repairList.stream().map(item -> item.get("id").toString()).collect(Collectors.toList());
            baseMapper.updateReceive(idList, null, 20, 2, null);
        }
    }

    @Override
    public List<Map<String, Object>> getList(String account) {
        Assert.notEmpty(account, "账号不能为空");
        return baseMapper.getList(account);
    }

    @Override
    public void operation(Map<String, Object> condition) {
        List<String> idList = (List<String>) condition.get("idList");
        Integer operation = (Integer) condition.get("operation");

        Assert.notEmpty(idList, "id列表不能为空");
        Assert.notEmpty(operation, "操作类型不能为空");

        baseMapper.updateReceive(idList, null, null, operation, null);
    }

    @Override
    public List<Map<String, Object>> flowDetails(Map<String, String> condition) {
        String flowLinkId = condition.get("flowLinkId");
        return baseMapper.flowDetails(flowLinkId);
    }

    @Override
    public void handleFlow(Map<String, String> condition) {
        // 操作人id
        String checkUserId = condition.get("checkUserId");
        // 流程关联id
        String flowLinkId = condition.get("flowLinkId");
        // 2=审批通过，3=审批驳回
        String checkState = condition.get("checkState");
        // 审批意见
        String suggestions = condition.get("suggestions");

        // 发起审批流
        baseMapper.insertFlowApplyCheck(
                IdWorker.getIdStr(),
                "202208240000000000000000001",
                "20220824000000000000000000102",
                "20220824000000000000000000101",
                null,
                checkUserId,
                checkState,
                suggestions,
                flowLinkId,
                "物料接收"
        );

        baseMapper.updatePickingStatus(flowLinkId, checkState);

    }

    @Override
    public List<Map<String, Object>> getReceiveBackupList(String account) {



        return null;
    }

}
