package com.zt.plat.module.qms.resource.device.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import com.zt.plat.module.qms.resource.device.controller.vo.DevicePeriodCheckPlanPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DevicePeriodCheckPlanRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DevicePeriodCheckPlanSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.device.dal.dataobject.DevicePeriodCheckPlanDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.device.dal.mapper.DevicePeriodCheckPlanMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.DEVICE_PERIOD_CHECK_PLAN_NOT_EXISTS;

/**
 * 设备-期间核查计划 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DevicePeriodCheckPlanServiceImpl implements DevicePeriodCheckPlanService {

    @Resource private DevicePeriodCheckPlanMapper devicePeriodCheckPlanMapper;
    @Resource private DevicePeriodCheckService devicePeriodCheckService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DevicePeriodCheckPlanRespVO createPlan(String checkYear) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        String userName = SecurityFrameworkUtils.getLoginUserNickname();

        String title = userName + "的"+checkYear+"年度期间核查计划";
        DevicePeriodCheckPlanDO entity = new DevicePeriodCheckPlanDO();
        entity.setCheckYear(checkYear);
        entity.setTitle(title);
        entity.setApplyUserName(userName);
        entity.setFlowStatus(QmsCommonConstant.NOT_START);
        devicePeriodCheckPlanMapper.insert(entity);
        devicePeriodCheckService.createListByPlan(entity);
        return BeanUtils.toBean(entity, DevicePeriodCheckPlanRespVO.class);
    }

    @Override
    public void updateDevicePeriodCheckPlan(DevicePeriodCheckPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateDevicePeriodCheckPlanExists(updateReqVO.getId());
        // 更新
        DevicePeriodCheckPlanDO updateObj = BeanUtils.toBean(updateReqVO, DevicePeriodCheckPlanDO.class);
        devicePeriodCheckPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevicePeriodCheckPlan(Long id) {
        // 校验存在
        validateDevicePeriodCheckPlanExists(id);
        // 删除
        devicePeriodCheckPlanMapper.deleteById(id);
    }

    @Override
        public void deleteDevicePeriodCheckPlanListByIds(List<Long> ids) {
        // 校验存在
        validateDevicePeriodCheckPlanExists(ids);
        // 删除
        devicePeriodCheckPlanMapper.deleteByIds(ids);
        }

    private void validateDevicePeriodCheckPlanExists(List<Long> ids) {
        List<DevicePeriodCheckPlanDO> list = devicePeriodCheckPlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_PERIOD_CHECK_PLAN_NOT_EXISTS);
        }
    }

    private void validateDevicePeriodCheckPlanExists(Long id) {
        if (devicePeriodCheckPlanMapper.selectById(id) == null) {
            throw exception(DEVICE_PERIOD_CHECK_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public DevicePeriodCheckPlanDO getDevicePeriodCheckPlan(Long id) {
        return devicePeriodCheckPlanMapper.selectById(id);
    }

    @Override
    public PageResult<DevicePeriodCheckPlanDO> getDevicePeriodCheckPlanPage(DevicePeriodCheckPlanPageReqVO pageReqVO) {
        return devicePeriodCheckPlanMapper.selectPage(pageReqVO);
    }

}