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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigUserSignaturePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigUserSignatureRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigUserSignatureSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigUserSignatureDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.business.config.dal.mapper.ConfigUserSignatureMapper;

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.CONFIG_USER_SIGNATURE_NOT_EXISTS;

/**
 * 手写签名配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigUserSignatureServiceImpl implements ConfigUserSignatureService {

    @Resource
    private ConfigUserSignatureMapper configUserSignatureMapper;

    @Override
    public ConfigUserSignatureDO getByUserId(Long userId) {
        LambdaQueryWrapper<ConfigUserSignatureDO> query = new LambdaQueryWrapper<>();
        query.eq(ConfigUserSignatureDO::getUserId, userId)
                .eq(ConfigUserSignatureDO::getCancelFlag, 0);
        query.orderByDesc(ConfigUserSignatureDO::getUpdateTime);
        List<ConfigUserSignatureDO> list = configUserSignatureMapper.selectList(query);
        return CollUtil.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public List<ConfigUserSignatureDO> getByIdList(List<Long> ids) {
        LambdaQueryWrapper<ConfigUserSignatureDO> query = new LambdaQueryWrapper<>();
        query.in(ConfigUserSignatureDO::getId, ids);
        return configUserSignatureMapper.selectList(query);
    }

    @Override
    public ConfigUserSignatureRespVO createConfigUserSignature(ConfigUserSignatureSaveReqVO createReqVO) {
        // 插入
        ConfigUserSignatureDO configUserSignature = BeanUtils.toBean(createReqVO, ConfigUserSignatureDO.class);
        configUserSignatureMapper.insert(configUserSignature);
        // 返回
        return BeanUtils.toBean(configUserSignature, ConfigUserSignatureRespVO.class);
    }

    @Override
    public void updateConfigUserSignature(ConfigUserSignatureSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigUserSignatureExists(updateReqVO.getId());
        // 更新
        ConfigUserSignatureDO updateObj = BeanUtils.toBean(updateReqVO, ConfigUserSignatureDO.class);
        configUserSignatureMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigUserSignature(Long id) {
        // 校验存在
        validateConfigUserSignatureExists(id);
        // 删除
        configUserSignatureMapper.deleteById(id);
    }

    @Override
        public void deleteConfigUserSignatureListByIds(List<Long> ids) {
        // 校验存在
        validateConfigUserSignatureExists(ids);
        // 删除
        configUserSignatureMapper.deleteByIds(ids);
        }

    private void validateConfigUserSignatureExists(List<Long> ids) {
        List<ConfigUserSignatureDO> list = configUserSignatureMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_USER_SIGNATURE_NOT_EXISTS);
        }
    }

    private void validateConfigUserSignatureExists(Long id) {
        if (configUserSignatureMapper.selectById(id) == null) {
            throw exception(CONFIG_USER_SIGNATURE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigUserSignatureDO getConfigUserSignature(Long id) {
        return configUserSignatureMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigUserSignatureDO> getConfigUserSignaturePage(ConfigUserSignaturePageReqVO pageReqVO) {
        return configUserSignatureMapper.selectPage(pageReqVO);
    }

}