package org.springblade.manager.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.business.entity.ConstructionLedger;
import org.springblade.business.mapper.ConstructionLedgerMapper;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.manager.dto.WbsTreeContractDTO;
import org.springblade.manager.entity.WbsTreeContract;
import org.springblade.manager.entity.WbsTreePrivate;
import org.springblade.manager.mapper.WbsTreeContractMapper;
import org.springblade.manager.mapper.WbsTreeMapper;
import org.springblade.manager.mapper.WbsTreePrivateMapper;
import org.springblade.manager.service.IWbsTreeContractService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@AllArgsConstructor
public class WbsTreeContractServiceImpl
        extends BaseServiceImpl<WbsTreeContractMapper, WbsTreeContract>
        implements IWbsTreeContractService {

    private final BladeLogger logger;
    private final WbsTreePrivateServiceImpl wbsTreePrivateService;
    private final ConstructionLedgerMapper constructionLedgerMapper;
    private final WbsTreePrivateMapper wbsTreePrivateMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitWbsTreeInContract(WbsTreeContractDTO pawDTO) {
        String wbsTreeIds = pawDTO.getWbsTreeIds();
        if (StringUtils.isEmpty(wbsTreeIds)) {
            return false;
        }
        String[] ids = wbsTreeIds.split(",");
        List<String> idList1 = Arrays.asList(ids);
        List<String> idList2 = new ArrayList<>();
        //查找合同段wbs私有库的节点树
        List<WbsTreeContract> list = baseMapper.findAllNodeList2(pawDTO.getProjectId(), pawDTO.getContractId(), pawDTO.getWbsId());
        for (WbsTreeContract wbsTreeContract : list) {
            idList2.add(String.valueOf(wbsTreeContract.getId()));
        }
        //比较项目wbs私有树与合同段wbs树是否相同,相同则不修改,不同则修改
        List<String> diffrent = getDiffrent(idList1, idList2);
//        diffrent.forEach(System.out::print);
        if (diffrent.size() == 0) { //相同
            return false;
        } else { //不相同
            if (idList1.size() > idList2.size()) {  //根据ids新增多余合同段wbs树节点
                for (String id : diffrent) {
                    //合同段wbs节点库存在该节点且为删除状态,那么修改is_deleted=0,否则新增
                    WbsTreeContract wbsTreeContract = baseMapper.selectByCondition2(id, pawDTO.getProjectId(), pawDTO.getContractId(), pawDTO.getWbsId());
                    if (wbsTreeContract != null) {
                        baseMapper.updateByCondition(id, pawDTO.getProjectId(), pawDTO.getContractId(), pawDTO.getWbsId());
                    } else {
                        //根据id、projectId获取WbsTreePrivate对象
                        WbsTreePrivate wbsTreePrivate = wbsTreePrivateMapper.selectByCondition(id, pawDTO.getProjectId());
                        Integer row = baseMapper.insertWbsTreeContract1(pawDTO.getContractId(), wbsTreePrivate);
                        if (row > 0) {
                            logger.info(wbsTreePrivate.getId().toString(), "初始化合同段wbs树成功");
                        }
                        //初始化施工台账数据
                        if (wbsTreePrivate.getDeptCategory() == 6
                                && wbsTreePrivate.getDeptCategory() != null
                                && !("").equals(wbsTreePrivate.getDeptCategory())) {
                            ConstructionLedger constructionLedger = new ConstructionLedger();
                            constructionLedger.setIsBeton(0);
                            constructionLedger.setWbsId(Long.parseLong(pawDTO.getWbsId()));
                            constructionLedger.setContractId(Long.parseLong(pawDTO.getContractId()));
                            int b2 = constructionLedgerMapper.insert(constructionLedger);
                            if (b2 > 0) {
                                logger.info(constructionLedger.getId().toString(), "初始化施工台账成功");
                            }
                        }
                    }
                }
            } else { //根据id、wbsID、projectId、contractId逻辑删除当前多余的合同段wbs树节点
                for (String id : diffrent) {
                    baseMapper.deleteByCondition(id, pawDTO.getWbsId(), pawDTO.getProjectId(), pawDTO.getContractId());
                }
            }
        }
        //TODO 初始化元素表、公式
        return true;
    }

    //比较两个List不同值
    public static List<String> getDiffrent(List<String> list1, List<String> list2) {
        List<String> diff = new ArrayList<String>();
        List<String> maxList = list1;
        List<String> minList = list2;
        if (list2.size() > list1.size()) {
            maxList = list2;
            minList = list1;
        }
        Map<String, Integer> map = new HashMap<String, Integer>(maxList.size());
        for (String string : maxList) {
            map.put(string, 1);
        }
        for (String string : minList) {
            if (map.get(string) != null) {
                map.put(string, 2);
                continue;
            }
            diff.add(string);
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        return diff;
    }

    private List<WbsTreeContract> findAllNodeList(String wbsTreeIds, String projectId, String contractId, String wbsId) {
        String[] ids = wbsTreeIds.split(",");
        List<Long> idList = new ArrayList<>();
        List<WbsTreeContract> list = new ArrayList<>();
        for (String id : ids) {
            idList.add(Long.valueOf(id));
            WbsTreeContract contract = baseMapper.selectByCondition(id, projectId, contractId, wbsId);
            list.add(contract);
        }
        return list;
    }


}