package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.common.exception.ServiceException;
import com.dsk.cscec.constant.CbProjectConstants;
import com.dsk.cscec.domain.CbProjectFile;
import com.dsk.cscec.domain.CbSummary;
import com.dsk.cscec.domain.CbSummaryActual;
import com.dsk.cscec.domain.bo.CbSummaryActualBo;
import com.dsk.cscec.domain.bo.CbSummaryActualListBo;
import com.dsk.cscec.domain.bo.CbSummaryBo;
import com.dsk.cscec.domain.vo.CbSummaryActualListVo;
import com.dsk.cscec.mapper.CbProjectFileMapper;
import com.dsk.cscec.mapper.CbSummaryActualMapper;
import com.dsk.cscec.mapper.CbSummaryMapper;
import com.dsk.cscec.service.CbSummaryService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * 成本汇总(CbSummary)表服务实现类
 *
 * @author cyf
 * @since 2024-02-06
 */
@Service
public class CbSummaryServiceImpl extends ServiceImpl<CbSummaryMapper, CbSummary> implements CbSummaryService {
    @Autowired
    private CbProjectFileMapper cbProjectFileMapper;

    @Autowired
    private CbSummaryActualMapper cbSummaryActualMapper;

    @Override
    public void importCbProject(Long projectId, Integer cbStage) {
        //获取文件信息
        List<CbProjectFile> cbProjectFiles = cbProjectFileMapper.selectList(new LambdaQueryWrapper<CbProjectFile>()
                .eq(CbProjectFile::getProjectId, projectId)
                .eq(CbProjectFile::getCbStage, cbStage)
                .eq(CbProjectFile::getCbType, CbProjectConstants.CB_TYPE_SUMMARY)
        );
        //解析数据
        cbProjectFiles.forEach(cbProjectFile -> {
            MultipartFile file = null;//todo
            if (cbProjectFile.getFileName().contains("项目")) {

            } else if (cbProjectFile.getFileName().contains("项目")) {

            } else {
                //修改文件状态
                cbProjectFile.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSE_FAIL);
                cbProjectFile.setFailRemark("文件名错误！");
                cbProjectFileMapper.updateById(cbProjectFile);
            }
        });

    }

    @Override
    public List<Map<String, Object>> getCbNameList(CbSummaryBo bo) {
        List<Map<String, Object>> resultList = new ArrayList<>();

        //项目汇总
        if (1 == bo.getCbType()) {
            //获取一级数据
            bo.setLevel(1);
            List<Map<String, Object>> cbNames = baseMapper.selectByLevel(bo);

            if (CollectionUtil.isEmpty(cbNames)) {
                return null;
            }
            Map<String, Object> xmhz = new HashMap<>();
            xmhz.put("id", 0l);
            xmhz.put("cbName", "成本汇总");
            resultList.add(xmhz);
            resultList.addAll(cbNames);
        }
        //成本科目汇总
        if (2 == bo.getCbType()) {
            bo.setLevel(0);
            List<Map<String, Object>> cbNameTop = baseMapper.selectByLevel(bo);

            bo.setLevel(1);
            List<Map<String, Object>> cbNames = baseMapper.selectByLevel(bo);
            resultList.addAll(cbNameTop);//todo
            resultList.addAll(cbNames);
        }

        return resultList;
    }

    @Override
    public List<CbSummaryActualListVo> getActualList(CbSummaryActualListBo bo) {

        //项目汇总
        if (1 == bo.getCbType()) {
            return getProjectSumList(bo);
        }
        //成本科目汇总
        if (2 == bo.getCbType()) {
            //todo
        }
        return null;
    }

    /**
     * 项目汇总列表
     *
     * @param bo
     * @return
     */
    private List<CbSummaryActualListVo> getProjectSumList(CbSummaryActualListBo bo) {
        CbSummary cbSummary = baseMapper.selectById(bo.getId());
        if (ObjectUtil.isEmpty(cbSummary)) {
            return null;
        }
        if (StringUtil.isBlank(bo.getExpenseDate())) {
            //默认当前月
            bo.setExpenseDate(DatePattern.SIMPLE_MONTH_FORMAT.format(new Date()));
        }
        List<CbSummaryActualListVo> childrenList = baseMapper.getByParentId(bo.getId(), bo.getExpenseDate());
        if (0 == cbSummary.getLevel()) {
            return childrenList;
        } else {
            childrenList.forEach(children -> {
                CbSummaryActualListBo childBo = new CbSummaryActualListBo();
                childBo.setId(children.getId());
                childBo.setExpenseDate(bo.getExpenseDate());
                children.setChildren(getProjectSumList(childBo));
            });

            return childrenList;
        }

    }

    @Override
    public List<String> getExpenseDateList(CbSummaryBo bo) {
        return baseMapper.getExpenseDateList(bo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertOrUpdateActual(List<CbSummaryActualBo> boList) {
        if (CollectionUtil.isEmpty(boList)) {
            return false;
        }
        List<CbSummaryActual> actualList = BeanUtil.copyToList(boList, CbSummaryActual.class);

        CbSummaryActual actualUpdate = actualList.get(0);
        CbSummaryActual actual = cbSummaryActualMapper.selectOne(new LambdaQueryWrapper<CbSummaryActual>()
                .eq(CbSummaryActual::getCbSummaryId, actualUpdate.getCbSummaryId())
                .eq(CbSummaryActual::getExpenseDate, actualUpdate.getExpenseDate())
                .eq(CbSummaryActual::getDelFlag, 0)
        );
        if (ObjectUtil.isNotNull(actual) && 1 == actual.getLockStatus()) {
            throw new ServiceException("当前成本已锁定，不允许编辑！");
        }

        return cbSummaryActualMapper.insertOrUpdateBatch(actualList) > 0;
    }
}
