package com.zt.plat.module.base.service.tmpltp;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.base.controller.admin.templtp.vo.DepartmentInstanceRelativityPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.DepartmentInstanceRelativityRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.DepartmentInstanceRelativitySaveReqVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.DepartmentInstanceRelativityDO;
import com.zt.plat.module.base.dal.mysql.tmpltp.DepartmentInstanceRelativityMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;



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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.DEPARTMENT_INSTANCE_RELATIVITY_NOT_EXISTS;


/**
 * 部门与实例关联 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
@Slf4j
public class DepartmentInstanceRelativityServiceImpl implements DepartmentInstanceRelativityService {

    @Resource
    private DepartmentInstanceRelativityMapper departmentInstanceRelativityMapper;

    @Override
    public DepartmentInstanceRelativityRespVO createDepartmentInstanceRelativity(DepartmentInstanceRelativitySaveReqVO createReqVO) {
        // 插入
        DepartmentInstanceRelativityDO departmentInstanceRelativity = BeanUtils.toBean(createReqVO, DepartmentInstanceRelativityDO.class);
        departmentInstanceRelativityMapper.insert(departmentInstanceRelativity);
        // 返回
        log.info("创建部门与实例关联成功，结果：{}", departmentInstanceRelativity);
        return BeanUtils.toBean(departmentInstanceRelativity, DepartmentInstanceRelativityRespVO.class);
    }

    @Override
    public void updateDepartmentInstanceRelativity(DepartmentInstanceRelativitySaveReqVO updateReqVO) {
        // 校验存在
        validateDepartmentInstanceRelativityExists(updateReqVO.getId());
        // 更新
        DepartmentInstanceRelativityDO updateObj = BeanUtils.toBean(updateReqVO, DepartmentInstanceRelativityDO.class);
        departmentInstanceRelativityMapper.updateById(updateObj);
        log.info("更新部门与实例关联成功，结果：{}", updateObj);
    }

    @Override
    public void deleteDepartmentInstanceRelativity(String id) {
        // 校验存在
        validateDepartmentInstanceRelativityExists(id);
        // 删除
        departmentInstanceRelativityMapper.deleteById(id);
        log.info("删除部门与实例关联成功，结果：{}", id);
    }

    @Override
        public void deleteDepartmentInstanceRelativityListByIds(List<Long> ids) {
        // 校验存在
        validateDepartmentInstanceRelativityExists(ids);
        // 删除
        departmentInstanceRelativityMapper.deleteByIds(ids);
        log.info("删除部门与实例关联成功，结果：{}", ids);
        }

    private void validateDepartmentInstanceRelativityExists(List<Long> ids) {
        List<DepartmentInstanceRelativityDO> list = departmentInstanceRelativityMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEPARTMENT_INSTANCE_RELATIVITY_NOT_EXISTS);
        }
    }

    private void validateDepartmentInstanceRelativityExists(String id) {
        if (departmentInstanceRelativityMapper.selectById(id) == null) {
            throw exception(DEPARTMENT_INSTANCE_RELATIVITY_NOT_EXISTS);
        }
    }

    @Override
    public DepartmentInstanceRelativityDO getDepartmentInstanceRelativity(String id) {
        return departmentInstanceRelativityMapper.selectById(id);
    }

    @Override
    public PageResult<DepartmentInstanceRelativityDO> getDepartmentInstanceRelativityPage(DepartmentInstanceRelativityPageReqVO pageReqVO) {
        return departmentInstanceRelativityMapper.selectPage(pageReqVO);
    }

}
