package com.xjrsoft.module.personnel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.module.personnel.dto.BasePersonnelLabourCapitalMonthPageDto;
import com.xjrsoft.module.personnel.dto.BasePersonnelLabourCapitalYearPageDto;
import com.xjrsoft.module.personnel.entity.BasePersonnelLabourCapital;
import com.xjrsoft.module.personnel.entity.BasePersonnelLabourCapitalData;
import com.xjrsoft.module.personnel.entity.BasePersonnelLabourCapitalTitle;
import com.xjrsoft.module.personnel.mapper.BasePersonnelLabourCapitalDataMapper;
import com.xjrsoft.module.personnel.mapper.BasePersonnelLabourCapitalMapper;
import com.xjrsoft.module.personnel.mapper.BasePersonnelLabourCapitalTitleMapper;
import com.xjrsoft.module.personnel.service.IBasePersonnelLabourCapitalService;
import com.xjrsoft.module.personnel.vo.BasePersonnelLabourCapitalMonthPageVo;
import com.xjrsoft.module.personnel.vo.BasePersonnelLabourCapitalYearPageVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @title: 工资发放
* @Author dzx
* @Date: 2023-11-08
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class BasePersonnelLabourCapitalServiceImpl extends MPJBaseServiceImpl<BasePersonnelLabourCapitalMapper, BasePersonnelLabourCapital> implements IBasePersonnelLabourCapitalService {
    private final BasePersonnelLabourCapitalMapper basePersonnelLabourCapitalBasePersonnelLabourCapitalMapper;

    private final BasePersonnelLabourCapitalDataMapper basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper;
    private final BasePersonnelLabourCapitalTitleMapper basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper;

    @Override
    public Page<BasePersonnelLabourCapitalMonthPageVo> getMonthPage(Page<BasePersonnelLabourCapitalMonthPageDto> page, BasePersonnelLabourCapitalMonthPageDto dto) {
        Page<BasePersonnelLabourCapitalMonthPageVo> resultPage = basePersonnelLabourCapitalBasePersonnelLabourCapitalMapper.getMonthPage(page, dto);

        for (BasePersonnelLabourCapitalMonthPageVo record : resultPage.getRecords()) {
            JSONObject extendJsonObj = JSON.parseObject(record.getPendingJson());
            Map<String, Object> newDict = new HashMap<>();

            for (String key : extendJsonObj.keySet()) {
                String bpName = getBpName(key);
                if (bpName != null) {
                    newDict.put(bpName, extendJsonObj.getString(key));
                }
            }

            record.setDict(newDict);
        }

        return resultPage;
    }

    @Override
    public Page<BasePersonnelLabourCapitalYearPageVo> getYearPage(Page<BasePersonnelLabourCapitalYearPageDto> page, BasePersonnelLabourCapitalYearPageDto dto) {
        return basePersonnelLabourCapitalBasePersonnelLabourCapitalMapper.getYearPage(page, dto);
    }

    private String getBpName(String columnNumber) {
        List<BasePersonnelLabourCapitalTitle> bpTitles = basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.selectList(Wrappers.<BasePersonnelLabourCapitalTitle>lambdaQuery().eq(BasePersonnelLabourCapitalTitle::getColumnNumber, columnNumber));
        if (bpTitles != null && !bpTitles.isEmpty()) {
            return bpTitles.get(0).getName();
        } else {
            return null;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BasePersonnelLabourCapital basePersonnelLabourCapital) {
        basePersonnelLabourCapitalBasePersonnelLabourCapitalMapper.insert(basePersonnelLabourCapital);
        for (BasePersonnelLabourCapitalData basePersonnelLabourCapitalData : basePersonnelLabourCapital.getBasePersonnelLabourCapitalDataList()) {
            basePersonnelLabourCapitalData.setLabourCapitalId(basePersonnelLabourCapital.getId());
            basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper.insert(basePersonnelLabourCapitalData);
        }
        for (BasePersonnelLabourCapitalTitle basePersonnelLabourCapitalTitle : basePersonnelLabourCapital.getBasePersonnelLabourCapitalTitleList()) {
            basePersonnelLabourCapitalTitle.setLabourCapitalId(basePersonnelLabourCapital.getId());
            basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.insert(basePersonnelLabourCapitalTitle);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(BasePersonnelLabourCapital basePersonnelLabourCapital) {
        basePersonnelLabourCapitalBasePersonnelLabourCapitalMapper.updateById(basePersonnelLabourCapital);
        //********************************* BasePersonnelLabourCapitalData  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BasePersonnelLabourCapitalData> basePersonnelLabourCapitalDataList = basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper.selectList(Wrappers.lambdaQuery(BasePersonnelLabourCapitalData.class).eq(BasePersonnelLabourCapitalData::getLabourCapitalId, basePersonnelLabourCapital.getId()).select(BasePersonnelLabourCapitalData::getId));
            List<Long> basePersonnelLabourCapitalDataIds = basePersonnelLabourCapitalDataList.stream().map(BasePersonnelLabourCapitalData::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> basePersonnelLabourCapitalDataOldIds = basePersonnelLabourCapital.getBasePersonnelLabourCapitalDataList().stream().map(BasePersonnelLabourCapitalData::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> basePersonnelLabourCapitalDataRemoveIds = basePersonnelLabourCapitalDataIds.stream().filter(item -> !basePersonnelLabourCapitalDataOldIds.contains(item)).collect(Collectors.toList());

            for (BasePersonnelLabourCapitalData basePersonnelLabourCapitalData : basePersonnelLabourCapital.getBasePersonnelLabourCapitalDataList()) {
                //如果不等于空则修改
                if (basePersonnelLabourCapitalData.getId() != null) {
                    basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper.updateById(basePersonnelLabourCapitalData);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    basePersonnelLabourCapitalData.setLabourCapitalId(basePersonnelLabourCapital.getId());
                    basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper.insert(basePersonnelLabourCapitalData);
                }
            }
            //已经不存在的id 删除
            if(basePersonnelLabourCapitalDataRemoveIds.size() > 0){
                basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper.deleteBatchIds(basePersonnelLabourCapitalDataRemoveIds);
            }
        }
        //********************************* BasePersonnelLabourCapitalData  增删改  结束 *******************************************/

        //********************************* BasePersonnelLabourCapitalTitle  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<BasePersonnelLabourCapitalTitle> basePersonnelLabourCapitalTitleList = basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.selectList(Wrappers.lambdaQuery(BasePersonnelLabourCapitalTitle.class).eq(BasePersonnelLabourCapitalTitle::getLabourCapitalId, basePersonnelLabourCapital.getId()).select(BasePersonnelLabourCapitalTitle::getId));
            List<Long> basePersonnelLabourCapitalTitleIds = basePersonnelLabourCapitalTitleList.stream().map(BasePersonnelLabourCapitalTitle::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> basePersonnelLabourCapitalTitleOldIds = basePersonnelLabourCapital.getBasePersonnelLabourCapitalTitleList().stream().map(BasePersonnelLabourCapitalTitle::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> basePersonnelLabourCapitalTitleRemoveIds = basePersonnelLabourCapitalTitleIds.stream().filter(item -> !basePersonnelLabourCapitalTitleOldIds.contains(item)).collect(Collectors.toList());

            for (BasePersonnelLabourCapitalTitle basePersonnelLabourCapitalTitle : basePersonnelLabourCapital.getBasePersonnelLabourCapitalTitleList()) {
                //如果不等于空则修改
                if (basePersonnelLabourCapitalTitle.getId() != null) {
                    basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.updateById(basePersonnelLabourCapitalTitle);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    basePersonnelLabourCapitalTitle.setLabourCapitalId(basePersonnelLabourCapital.getId());
                    basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.insert(basePersonnelLabourCapitalTitle);
                }
            }
            //已经不存在的id 删除
            if(basePersonnelLabourCapitalTitleRemoveIds.size() > 0){
                basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.deleteBatchIds(basePersonnelLabourCapitalTitleRemoveIds);
            }
        }
        //********************************* BasePersonnelLabourCapitalTitle  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        basePersonnelLabourCapitalBasePersonnelLabourCapitalMapper.deleteBatchIds(ids);
        basePersonnelLabourCapitalBasePersonnelLabourCapitalDataMapper.delete(Wrappers.lambdaQuery(BasePersonnelLabourCapitalData.class).in(BasePersonnelLabourCapitalData::getLabourCapitalId, ids));
        basePersonnelLabourCapitalBasePersonnelLabourCapitalTitleMapper.delete(Wrappers.lambdaQuery(BasePersonnelLabourCapitalTitle.class).in(BasePersonnelLabourCapitalTitle::getLabourCapitalId, ids));

        return true;
    }
}
