package com.unis.module.coding.service.cwxx;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.yitter.idgen.YitIdHelper;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.util.object.BeanUtils;
import com.unis.framework.security.core.LoginUser;
import com.unis.framework.security.core.util.SecurityFrameworkUtils;
import com.unis.module.coding.controller.admin.cwxx.vo.CwxxPageReqVO;
import com.unis.module.coding.controller.admin.cwxx.vo.CwxxRespVO;
import com.unis.module.coding.controller.admin.cwxx.vo.CwxxSaveReqVO;
import com.unis.module.coding.controller.admin.cwxxyear.vo.CwxxYearSaveReqVO;
import com.unis.module.coding.dal.dataobject.cwxx.CwxxDO;
import com.unis.module.coding.dal.dataobject.cwxxyear.CwxxYearDO;
import com.unis.module.coding.dal.dataobject.kqxx.KqxxDO;
import com.unis.module.coding.dal.mysql.cwxx.CwxxMapper;
import com.unis.module.coding.dal.mysql.cwxxyear.CwxxYearMapper;
import com.unis.module.coding.dal.mysql.kqxx.KqxxMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.Year;
import java.util.List;

import static com.unis.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.unis.module.core.enums.ErrorCodeConstants.CWXX_NOT_EXISTS;
import static com.unis.module.infra.enums.ErrorCodeConstants.ERROR;

/**
 * 财务信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class CwxxServiceImpl implements CwxxService {

    @Resource
    private CwxxMapper cwxxMapper;
    @Resource
    private CwxxYearMapper cwxxYearMapper;
    @Resource
    private KqxxMapper kqxxMapper;

    @Override
    public Long createCwxx(CwxxSaveReqVO createReqVO) {
        //财务信息根据单位绑定,有就修改,没有就添加
        CwxxDO cwxx = BeanUtils.toBean(createReqVO, CwxxDO.class);
//        List<CwxxDO> dept_id = cwxxMapper.selectList(new QueryWrapper<CwxxDO>().eq("DEPT_ID", createReqVO.getDeptId()));
        // 插入
        cwxx.setId(YitIdHelper.nextId());
        cwxxMapper.insert(cwxx);
        // 返回
        return cwxx.getId();
    }


    @Override
    public void updateCwxx(CwxxSaveReqVO updateReqVO) {
        // 校验存在
        validateCwxxExists(updateReqVO.getId());
        // 更新
        CwxxDO updateObj = BeanUtils.toBean(updateReqVO, CwxxDO.class);
        cwxxMapper.updateById(updateObj);
    }

    @Override
    public void deleteCwxx(Long id) {
        // 校验存在
        validateCwxxExists(id);
        // 删除
        cwxxMapper.deleteById(id);
    }

    private void validateCwxxExists(Long id) {
        if (cwxxMapper.selectById(id) == null) {
            throw exception(CWXX_NOT_EXISTS);
        }
    }

    @Override
    public CwxxRespVO getCwxx(Long id) {
        CwxxDO cwxxDO = cwxxMapper.selectById(id);
        if (ObjectUtils.isEmpty(cwxxDO)) {
            throw exception(ERROR, "不存在财务信息");
        }
        CwxxRespVO cwxxRespVO = BeanUtils.toBean(cwxxDO, CwxxRespVO.class);
        List<CwxxYearDO> cwxxYearDOS = cwxxYearMapper.selectByCwxxId(id);
        if (!ObjectUtils.isEmpty(cwxxYearDOS)) {
            List<CwxxYearSaveReqVO> cwxxYearSaveReqVOS = BeanUtils.toBean(cwxxYearDOS, CwxxYearSaveReqVO.class);
            cwxxRespVO.setCwxxYearList(cwxxYearSaveReqVOS);
        }
        return cwxxRespVO;
    }

    @Override
    public PageResult<CwxxDO> getCwxxPage(CwxxPageReqVO pageReqVO) {
        return cwxxMapper.selectPage(pageReqVO);
    }

    @Override
    public CwxxRespVO getCwxxByKqBm() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        String kqbm = loginUser.getInfo().get("kqbm");
        if (ObjectUtils.isEmpty(loginUser)) {
            throw exception(ERROR, "不存在用户信息");
        }
        if (ObjectUtils.isEmpty(kqbm)) {
            throw exception(ERROR, "当前用户不存在库区编码");
        }
        //查询库区信息
        List<KqxxDO> kqbm1 = kqxxMapper.selectList(new QueryWrapper<KqxxDO>().eq("KQBM", kqbm));
        if (CollectionUtils.isEmpty(kqbm1)) {
            throw exception(ERROR, "当前库区编码下无库区信息");
        }
        //查询财务信息
        List<CwxxDO> cwxxDOS = cwxxMapper.selectList(new QueryWrapper<CwxxDO>().eq("KQID", kqbm1.get(0).getId()));
        if (CollectionUtils.isEmpty(cwxxDOS)) {
            throw exception(ERROR, "当前库区编码下无信息");
        }
//        int currentYear = Year.now().getValue();
//        int startYear = currentYear - 2; // 近三年的起始年份
//        int endYear = currentYear;
        //查询财务年的信息
        List<CwxxYearDO> cwxx_id = cwxxYearMapper.selectList(
                new QueryWrapper<CwxxYearDO>().eq("CWXX_ID", cwxxDOS.get(0).getId()));
        //财务信息和财务年信息的DO转成vo
        CwxxRespVO cwxxRespVO = BeanUtils.toBean(cwxxDOS.get(0), CwxxRespVO.class);
        List<CwxxYearSaveReqVO> cwxxYearSaveReqVOS = BeanUtils.toBean(cwxx_id, CwxxYearSaveReqVO.class);
        cwxxRespVO.setCwxxYearList(cwxxYearSaveReqVOS);
        return cwxxRespVO;

    }

}