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

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
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.emergency.vo.QhseEmergencyBookPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyBookSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.DeptUtil;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.emergency.QhseEmergencyBookDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.emergency.QhseEmergencyBookMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
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.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

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

/**
 * QHSE应检物资台账 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class QhseEmergencyBookServiceImpl implements QhseEmergencyBookService {

    @Resource
    private QhseEmergencyBookMapper qhseEmergencyBookMapper;
    @Autowired
    private DeptUtil deptUtil;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private DeptService deptService;

    @Override
    public Long createQhseEmergencyBook(QhseEmergencyBookSaveReqVO createReqVO) {
        // 插入
        QhseEmergencyBookDO qhseEmergencyBook = BeanUtils.toBean(createReqVO, QhseEmergencyBookDO.class);
        Long deptId = createReqVO.getDeptId();
        DeptRespDTO dept = deptApi.getDept(deptId);
        String companyCode = deptUtil.getCompanyCode(deptId);
        qhseEmergencyBook.setEmergencyCode(companyCode + PinyinUtil.getPinyin(dept.getName()).replace(" ", "") + DateUtil.format(new Date(), "yyMMddHHmmss"));
        qhseEmergencyBook.setDeleted(false);
        qhseEmergencyBookMapper.insert(qhseEmergencyBook);
        // 返回
        return qhseEmergencyBook.getId();
    }
    @Override
    public void updateQhseEmergencyBook(QhseEmergencyBookSaveReqVO updateReqVO) {
        // 校验存在
        validateQhseEmergencyBookExists(updateReqVO.getId());
        // 更新
        QhseEmergencyBookDO updateObj = BeanUtils.toBean(updateReqVO, QhseEmergencyBookDO.class);
        qhseEmergencyBookMapper.updateById(updateObj);
    }

    @Override
    public void deleteQhseEmergencyBook(Long id) {
        // 校验存在
        validateQhseEmergencyBookExists(id);
        // 删除
        qhseEmergencyBookMapper.deleteById(id);
    }

    private void validateQhseEmergencyBookExists(Long id) {
        if (qhseEmergencyBookMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2, "不存在"));
        }
    }

    @Override
    public QhseEmergencyBookDO getQhseEmergencyBook(Long id) {
        return qhseEmergencyBookMapper.selectById(id);
    }

    @Override
    public PageResult<QhseEmergencyBookDO> getQhseEmergencyBookPage(QhseEmergencyBookPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return qhseEmergencyBookMapper.selectPage(pageReqVO, ids);
    }

}