package cn.iocoder.yudao.module.bpm.service.stamp.stampseal;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.stamp.stampseal.vo.OaStampSealPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.stamp.stampseal.vo.OaStampSealSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.stamp.stampseal.OaStampSealDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.stamp.stampseal.OaStampSealMapper;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.OA_STAMP_SEAL_NOT_EXISTS;

/**
 * 用印印章信息 Service 实现类
 *
 * @author dp
 */
@Service
@Validated
public class OaStampSealServiceImpl implements OaStampSealService {

    @Resource
    private OaStampSealMapper oaStampSealMapper;

    @Resource
    private FileApi fileApi;

    @Override
    @Transactional
    public Long createOaStampSeal(OaStampSealSaveReqVO createReqVO) {
        // 插入
        OaStampSealDO oaStampSeal = BeanUtils.toBean(createReqVO, OaStampSealDO.class);
        oaStampSeal.setSealUuid(IdUtil.fastSimpleUUID());
        oaStampSealMapper.insert(oaStampSeal);

        // 保存业务uuid到附件中
        fileApi.updateFileBiz(createReqVO.getFileIdList(), oaStampSeal.getSealUuid());

        // 返回
        return oaStampSeal.getId();
    }

    @Override
    @Transactional
    public void updateOaStampSeal(OaStampSealSaveReqVO updateReqVO) {
        // 校验存在
        OaStampSealDO oaStampSealDO = validateOaStampSealExists(updateReqVO.getId());
        // 更新
        OaStampSealDO updateObj = BeanUtils.toBean(updateReqVO, OaStampSealDO.class);
        oaStampSealMapper.updateById(updateObj);

        // 保存业务uuid到附件中
        fileApi.updateFileBiz(updateReqVO.getFileIdList(), oaStampSealDO.getSealUuid());
    }

    @Override
    public void deleteOaStampSeal(Long id) {
        // 校验存在
        validateOaStampSealExists(id);
        // 删除
        oaStampSealMapper.deleteById(id);
    }

    @Override
    public void openOaStampSeal(Long id) {
        validateOaStampSealExists(id);
        OaStampSealDO build = OaStampSealDO.builder()
                .id(id)
                .status("1")
                .build();
        oaStampSealMapper.updateById(build);
    }

    @Override
    public void closeOaStampSeal(Long id) {
        validateOaStampSealExists(id);
        OaStampSealDO build = OaStampSealDO.builder()
                .id(id)
                .status("2")
                .build();
        oaStampSealMapper.updateById(build);

    }

    private OaStampSealDO validateOaStampSealExists(Long id) {
        OaStampSealDO oaStampSealDO = oaStampSealMapper.selectById(id);
        if (oaStampSealDO == null) {
            throw exception(OA_STAMP_SEAL_NOT_EXISTS);
        }
        return oaStampSealDO;
    }

    @Override
    public OaStampSealDO getOaStampSeal(Long id) {
        return oaStampSealMapper.selectById(id);
    }

    @Override
    public PageResult<OaStampSealDO> getOaStampSealPage(OaStampSealPageReqVO pageReqVO) {
        return oaStampSealMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OaStampSealDO> getOaStampSealList() {
        LambdaQueryWrapper<OaStampSealDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(OaStampSealDO::getStatus, "1") // 开启的
                .orderByAsc(OaStampSealDO::getId);
        return oaStampSealMapper.selectList(lambdaQueryWrapper);
    }



}