package com.chinaweal.aiccs.aiccs.punishment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinaweal.aiccs.aiccs.punishment.entity.EntLock;
import com.chinaweal.aiccs.aiccs.punishment.entity.Tolockentlist;
import com.chinaweal.aiccs.aiccs.punishment.job.Task;
import com.chinaweal.aiccs.aiccs.punishment.mapper.EntlockMapper;
import com.chinaweal.aiccs.aiccs.punishment.mapper.TolockentlistMapper;
import com.chinaweal.aiccs.aiccs.punishment.service.EntlockService;
import com.chinaweal.aiccs.aiccs.punishment.service.TolockentlistService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lee
 * @since 2021-06-05
 */
@Service
public class TolockentlistServiceImpl extends BaseServiceImpl<TolockentlistMapper, Tolockentlist> implements TolockentlistService {

    @Resource
    private TolockentlistMapper tolockentlistMapper;

    @Resource
    private EntlockService entlockService;

    @Resource
    private EntlockMapper entlockMapper;

    @Override
    public List<Tolockentlist> selectTolockentlistList() {
        return tolockentlistMapper.selectTolockentlistList();
    }

    @Override
    public void updateByTolockId(String tolockId) {
        baseMapper.updateByTolockId(tolockId);
    }

    @Override
    public List<Map> groupDepart(Map<String, Object> customParamMap) {
        String beginDate = "";
        //String endLockDate = "";
        //String beginUnlockDate = "";
        String endDate = "";
        if(customParamMap != null){
            beginDate = StringUtils.tranObject(customParamMap.get("beginDate"));
            //endLockDate = com.chinaweal.aiccs.common.util.StringUtils.tranObject(customParamMap.get("endLockDate"));
            //beginUnlockDate = com.chinaweal.aiccs.common.util.StringUtils.tranObject(customParamMap.get("beginUnlockDate"));
            endDate = StringUtils.tranObject(customParamMap.get("endDate"));
        }
        Map<String, String> departmentMap = Task.PunishFromAndDepartmentMap;
        List<Map> list = new ArrayList<>();
        Set<String> departIds = new HashSet<>(departmentMap.values());
        for (String departId : departIds) {
            Map<String, Object> map = new HashMap<>();

            map.put("departId", departId);

            //拟列入
            LambdaQueryWrapper<Tolockentlist> tlq = Wrappers.lambdaQuery();
            if(customParamMap==null){
                tlq.eq(Tolockentlist::getDepartment, departId)
                   .in(Tolockentlist::getState, "0", "1");
            }
            map.put("draft", count(tlq));
            String finalBeginDate = beginDate;
            String finalEndDate = endDate;
            //已锁定
            LambdaQueryWrapper<EntLock> lelq = Wrappers.lambdaQuery();
            LambdaQueryWrapper<EntLock> autolelq = Wrappers.lambdaQuery();
            LambdaQueryWrapper<EntLock> manuallelq = Wrappers.lambdaQuery();
            lelq.eq(EntLock::getDepartment, departId)
                    .in(EntLock::getState, "0", "2", "3")
                    .orderByDesc(EntLock::getLaupTime);
            if(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate)){
                lelq.and(LambdaQueryWrapper->LambdaQueryWrapper.between(EntLock::getLockTime, finalBeginDate, finalEndDate).or().between(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate),EntLock::getUnlockTime, finalBeginDate, finalEndDate));
            }


            autolelq.eq(EntLock::getDepartment, departId)
                    .eq(EntLock::getLockusername,"信用风险监管处")
                    .in(EntLock::getState, "0", "2", "3")
                    .orderByDesc(EntLock::getLaupTime);
            if(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate)){
                autolelq.and(LambdaQueryWrapper->LambdaQueryWrapper.between(EntLock::getLockTime, finalBeginDate, finalEndDate).or().between(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate),EntLock::getUnlockTime, finalBeginDate, finalEndDate));
            }

            manuallelq.eq(EntLock::getDepartment, departId)
                    .ne(EntLock::getLockusername,"信用风险监管处")
                    .in(EntLock::getState, "0", "2", "3")
                    .orderByDesc(EntLock::getLaupTime);
            if(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate)){
                manuallelq.and(LambdaQueryWrapper->LambdaQueryWrapper.between(EntLock::getLockTime, finalBeginDate, finalEndDate).or().between(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate),EntLock::getUnlockTime, finalBeginDate, finalEndDate));
            }

            map.put("locked", entlockService.count(lelq));
            map.put("autolocked", entlockService.count(autolelq));
            map.put("manuallocked", entlockService.count(manuallelq));

            //已解锁
            LambdaQueryWrapper<EntLock> ulelq = Wrappers.lambdaQuery();
            LambdaQueryWrapper<EntLock> autoulelq = Wrappers.lambdaQuery();
            LambdaQueryWrapper<EntLock> manualulelq = Wrappers.lambdaQuery();

            ulelq.eq(EntLock::getDepartment, departId)
                    .eq(EntLock::getState, "3")
                    .orderByDesc(EntLock::getLaupTime);
            if(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate)){
                ulelq.and(LambdaQueryWrapper->LambdaQueryWrapper.between(EntLock::getLockTime, finalBeginDate, finalEndDate).or().between(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate),EntLock::getUnlockTime, finalBeginDate, finalEndDate));
            }
            autoulelq.eq(EntLock::getDepartment, departId)
                    .eq(EntLock::getUnlusername,"信用风险监管处")
                    .eq(EntLock::getState, "3")
                    .orderByDesc(EntLock::getLaupTime);
            if(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate)){
                autoulelq.and(LambdaQueryWrapper->LambdaQueryWrapper.between(EntLock::getLockTime, finalBeginDate, finalEndDate).or().between(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate),EntLock::getUnlockTime, finalBeginDate, finalEndDate));
            }

            manualulelq.eq(EntLock::getDepartment, departId)
                    .ne(EntLock::getUnlusername,"信用风险监管处")
                    .eq(EntLock::getState, "3")
                    .orderByDesc(EntLock::getLaupTime);
            if(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate)){
                manualulelq.and(LambdaQueryWrapper->LambdaQueryWrapper.between(EntLock::getLockTime, finalBeginDate, finalEndDate).or().between(StringUtils.isNotBlank(finalBeginDate) && StringUtils.isNotBlank(finalEndDate),EntLock::getUnlockTime, finalBeginDate, finalEndDate));
            }

            map.put("unlocked", entlockService.count(ulelq));
            map.put("autounlocked", entlockService.count(autoulelq));
            map.put("manualunlocked", entlockService.count(manualulelq));


            //曾经锁定
            LambdaQueryWrapper<EntLock> elq = Wrappers.lambdaQuery();
            elq.eq(EntLock::getDepartment, departId)
                    .select(EntLock::getEntname)
                    .orderByDesc(EntLock::getLaupTime);
                    //.between(StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate),EntLock::getLockTime,beginDate,endDate)
                    //.between(StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate),EntLock::getUnlockTime,beginDate,endDate)
            map.put("handled",entlockService.count(elq));

            list.add(map);
        }

        return list;
    }
}
