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.lang.Assert;
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.CbProjectRecord;
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.CbProjectRecordMapper;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 成本汇总(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;

    @Autowired
    private CbProjectRecordMapper cbProjectRecordMapper;

    @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) {
        Assert.notNull(bo.getProjectId(), "项目id不能为空");
        Assert.notNull(bo.getCbStage(), "成本阶段不能为空");
        Assert.notNull(bo.getCbType(), "成本类型不能为空");

        List<Map<String, Object>> resultList = new ArrayList<>();

        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);
        resultList.addAll(cbNames);

        return resultList;
    }

    @Override
    public List<CbSummaryActualListVo> getActualList(CbSummaryActualListBo bo) {
        Assert.notNull(bo.getId(),"id不能为空");
        Assert.notNull(bo.getCbType(),"成本类型不能为空");
        if (StringUtil.isBlank(bo.getExpenseDate())) {
            //默认当前月
            bo.setExpenseDate(DatePattern.SIMPLE_MONTH_FORMAT.format(new Date()));
        }

        CbSummary cbSummary = baseMapper.selectById(bo.getId());
        if (ObjectUtil.isEmpty(cbSummary)) {
            return null;
        }

        List<CbSummaryActualListVo> childrenList = baseMapper.getByParentId(bo.getId(), bo.getExpenseDate());
        if (0 == cbSummary.getLevel() && 1 == bo.getCbType()) {
            //项目汇总
            return childrenList;
        }else if(0 == cbSummary.getLevel() && 2 == bo.getCbType()) {
            //成本科目汇总
            CbSummaryActualListVo cbSummaryActualListVo = baseMapper.getById(bo.getId(), bo.getExpenseDate());
            cbSummaryActualListVo.setChildren(childrenList);

            List<CbSummaryActualListVo> list = new ArrayList<>();
            list.add(cbSummaryActualListVo);
            return list;
        }else {
            childrenList = getProjectSumList(bo);
            CbSummaryActualListVo cbSummaryActualListVo = baseMapper.getById(bo.getId(),bo.getExpenseDate());
            cbSummaryActualListVo.setChildren(childrenList);

            List<CbSummaryActualListVo> list = new ArrayList<>();
            list.add(cbSummaryActualListVo);
            return list;
        }

    }

    /**
     * 成本汇总列表
     *
     * @param bo
     * @return
     */
    private List<CbSummaryActualListVo> getProjectSumList(CbSummaryActualListBo bo) {

        List<CbSummaryActualListVo> childrenList = baseMapper.getByParentId(bo.getId(), bo.getExpenseDate());
        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) {
        Assert.notNull(bo.getProjectId(),"项目id不能为空");
        Assert.notNull(bo.getCbStage(),"成本阶段不能为空");
        Assert.notNull(bo.getCbType(),"成本类型不能为空");

        return baseMapper.getExpenseDateList(bo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertOrUpdateActual(List<CbSummaryActualBo> boList) {
        if (CollectionUtil.isEmpty(boList)) {
            return false;
        }
        boList.forEach(bo->{
            Assert.notNull(bo.getCbSummaryId(),"成本汇总id不能为空");
            Assert.notNull(bo.getExpenseDate(),"费用日期不能为空");

        });
        List<CbSummaryActual> actualList = BeanUtil.copyToList(boList, CbSummaryActual.class);

        //todo
//        throw new ServiceException("当前成本已锁定，不允许编辑！");


        return cbSummaryActualMapper.insertOrUpdateBatch(actualList) > 0;
    }

    @Override
    public List<CbSummaryActualListVo> getAll(CbSummaryActualListBo bo) {
        Assert.notNull(bo.getId(), "项目id不能为空");
        Assert.notNull(bo.getCbType(), "成本类型不能为空");
        Assert.notNull(bo.getExpenseDate(), "费用日期不能为空");
        //获取项目成本阶段
        CbProjectRecord cbProjectRecord = cbProjectRecordMapper.selectById(bo.getId());
        if (ObjectUtil.isNull(cbProjectRecord)) {
            throw new ServiceException("当前项目不存在");
        }
        bo.setCbStage(cbProjectRecord.getCbStage());
        if (bo.getCbType() == 1) {
            return baseMapper.getProjectAll(bo);
        } else {
            return baseMapper.getCostAccountAll(bo);
        }
        //todo 截至本月费用汇总
    }
}
