package com.zt.plat.module.qms.business.reportdoc.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigPermissionSaveReqVO;
import com.zt.plat.module.qms.business.config.service.ConfigPermissionService;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.*;
import com.zt.plat.module.qms.business.reportdoc.dal.mapper.ReportDocumentTypeMapper;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentTypeDO;
import com.zt.plat.module.qms.enums.QmsPermissionConstant;
import com.zt.plat.module.system.api.permission.PermissionApi;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.REPORT_DOCUMENT_TYPE_NOT_EXISTS;

/**
 * 报告类型配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ReportDocumentTypeServiceImpl implements ReportDocumentTypeService {

    @Resource private ReportDocumentTypeMapper reportDocumentTypeMapper;
    @Resource private ConfigPermissionService configPermissionService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> save(ReportDocumentTypeSaveReqVO reqVo) {
        String newVersionFlag = reqVo.getNewVersionFlag();

        //处理权限
        List<ConfigPermissionSaveReqVO> primissionList = reqVo.getPermissionList();
        if(reqVo.getId() != null){
            configPermissionService.savePermissionListWithAutoDelete(primissionList, reqVo.getMainId(), QmsPermissionConstant.REPORT_DOCUMENT_TYPE_START, null);
        }
        if("1".equals(newVersionFlag))
            return saveWithNewVersion(reqVo);
        if(reqVo.getId() == null){
            ReportDocumentTypeDO typeDO = BeanUtils.toBean(reqVo, ReportDocumentTypeDO.class);
            typeDO.setCurrentFlag(1);
            reportDocumentTypeMapper.insert(typeDO);
            Long id = typeDO.getId();
            typeDO.setMainId(id);

            for(ConfigPermissionSaveReqVO permission : primissionList)
                permission.setSourceId( id);
            configPermissionService.savePermissionListWithAutoDelete(primissionList, id, QmsPermissionConstant.REPORT_DOCUMENT_TYPE_START, null);

            this.reportDocumentTypeMapper.updateById(typeDO);
        }
        else
            this.updateReportDocumentType(reqVo);
        return CommonResult.success("保存成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> saveWithNewVersion(ReportDocumentTypeSaveReqVO reqV) {
        ReportDocumentTypeDO documentTypeDO = BeanUtils.toBean(reqV, ReportDocumentTypeDO.class);
        documentTypeDO.setCurrentFlag(0);
        reportDocumentTypeMapper.updateById(documentTypeDO);
        //创建新版本
        Integer version = documentTypeDO.getVersion();
        if(version != null)
            version ++;
        else
            version = 1;
        documentTypeDO.setId(null);
        documentTypeDO.setVersion(version);
        documentTypeDO.setCurrentFlag(1);
        documentTypeDO.setMainId(reqV.getMainId());
        reportDocumentTypeMapper.insert(documentTypeDO);
        return CommonResult.success("保存成功");
    }

    @Override
    public ReportDocumentTypeRespVO createReportDocumentType(ReportDocumentTypeSaveReqVO createReqVO) {
        // 插入
        ReportDocumentTypeDO reportDocumentType = BeanUtils.toBean(createReqVO, ReportDocumentTypeDO.class);
        reportDocumentTypeMapper.insert(reportDocumentType);
        // 返回
        return BeanUtils.toBean(reportDocumentType, ReportDocumentTypeRespVO.class);
    }

    @Override
    public void updateReportDocumentType(ReportDocumentTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateReportDocumentTypeExists(updateReqVO.getId());
        // 更新
        ReportDocumentTypeDO updateObj = BeanUtils.toBean(updateReqVO, ReportDocumentTypeDO.class);
        reportDocumentTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteReportDocumentType(Long id) {
        // 校验存在
        validateReportDocumentTypeExists(id);
        // 删除
        reportDocumentTypeMapper.deleteById(id);
    }

    @Override
    public void deleteReportDocumentTypeListByIds(List<Long> ids) {
        // 校验存在
        validateReportDocumentTypeExists(ids);
        // 删除
        reportDocumentTypeMapper.deleteByIds(ids);
    }

    private void validateReportDocumentTypeExists(List<Long> ids) {
        List<ReportDocumentTypeDO> list = reportDocumentTypeMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(REPORT_DOCUMENT_TYPE_NOT_EXISTS);
        }
    }

    private void validateReportDocumentTypeExists(Long id) {
        if (reportDocumentTypeMapper.selectById(id) == null) {
            throw exception(REPORT_DOCUMENT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ReportDocumentTypeDO getReportDocumentType(Long id) {
        return reportDocumentTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ReportDocumentTypeDO> getReportDocumentTypePage(ReportDocumentTypePageReqVO pageReqVO) {
        return reportDocumentTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReportDocumentTypeDO> selectListWithPermission(ReportDocumentTypePageReqVO pageReqVO) {
        return reportDocumentTypeMapper.selectListWithPermission(pageReqVO);
    }
}