package cn.iocoder.yudao.module.pms.service.qhse.report;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.report.vo.QhseMonthReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.report.vo.QhseMonthReportRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.report.vo.QhseMonthReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.report.QhseMonthReportDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.report.QhseMonthReportMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 资料 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class QhseMonthReportServiceImpl implements QhseMonthReportService {

    @Resource
    private QhseMonthReportMapper qhseMonthReportMapper;
    @Autowired
    private DeptService deptService;

    @Override
    public Long createQhseMonthReport(QhseMonthReportSaveReqVO createReqVO) {
        // 插入
        QhseMonthReportDO qhseMonthReport = BeanUtils.toBean(createReqVO, QhseMonthReportDO.class);
        qhseMonthReport.setDeleted(false);
        qhseMonthReportMapper.insert(qhseMonthReport);
        // 返回
        return qhseMonthReport.getId();
    }

    @Override
    public void updateQhseMonthReport(QhseMonthReportSaveReqVO updateReqVO) {
        // 校验存在
        validateQhseMonthReportExists(updateReqVO.getId());
        // 更新
        QhseMonthReportDO updateObj = BeanUtils.toBean(updateReqVO, QhseMonthReportDO.class);
        qhseMonthReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteQhseMonthReport(Long id) {
        // 校验存在
        validateQhseMonthReportExists(id);
        // 删除
        qhseMonthReportMapper.deleteById(id);
    }

    private void validateQhseMonthReportExists(Long id) {
        if (qhseMonthReportMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1,"不存在"));
        }
    }

    @Override
    public QhseMonthReportDO getQhseMonthReport(Long id) {
        return qhseMonthReportMapper.selectById(id);
    }

    @Override
    public PageResult<QhseMonthReportDO> getQhseMonthReportPage(QhseMonthReportPageReqVO pageReqVO) {
        return qhseMonthReportMapper.selectPage(pageReqVO);
    }


    public PageResult<QhseMonthReportRespVO> getDistinctPage(QhseMonthReportPageReqVO pageReqVO) {
        IPage<QhseMonthReportRespVO> page;
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        page = qhseMonthReportMapper.getDistinctYearMonths(
                new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO, ids);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }
}