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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.tenant.core.context.CompanyContextHolder;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmpItmPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplItmRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplItmSaveReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.UpdateStsVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplItmDO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplTpFldDO;
import com.zt.plat.module.base.dal.mysql.tmpltp.TmplItmMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.TMPL_ITM_NAME_CODE_EXISTS;
import static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.TMPL_ITM_NOT_EXISTS;

@Service
@Validated
@Slf4j
public class TmplItmServiceImpl extends ServiceImpl<TmplItmMapper, TmplItmDO> implements TmplItmService{
    @Override
    public TmplItmRespVO createTmplItm(TmplItmSaveReqVO tmplItmSaveReqVO) {
        //验证条款名是否存在
        validateTmplLtmNameAndCodeExists(tmplItmSaveReqVO.getItmName(), tmplItmSaveReqVO.getItmNum());
        tmplItmSaveReqVO.setId(null);
        TmplItmDO bean = BeanUtils.toBean(tmplItmSaveReqVO, TmplItmDO.class);
        bean.setSts(1);
        baseMapper.insert(bean);
        return BeanUtils.toBean(bean, TmplItmRespVO.class);
    }

    @Override
    public void updateTmplItm(TmplItmSaveReqVO tmplItmSaveReqVO) {
        validateTmplLtmExists(tmplItmSaveReqVO.getId());
        TmplItmDO bean = BeanUtils.toBean(tmplItmSaveReqVO, TmplItmDO.class);
        this.updateById(bean);
    }


    private void validateTmplLtmExists(List<Long> ids) {
        List<TmplItmDO> list = baseMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TMPL_ITM_NOT_EXISTS);
        }
    }

    private void validateTmplLtmExists(Long id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(TMPL_ITM_NOT_EXISTS);
        }
    }
    private  void validateTmplLtmNameAndCodeExists(String name, String code){
        if (baseMapper.selectCount(new LambdaQueryWrapper<TmplItmDO>()
                .eq(TmplItmDO::getDeleted, 0)
                .eq(TmplItmDO::getCompanyId, CompanyContextHolder.getCompanyId())
                .and(wrapper -> wrapper.eq(TmplItmDO::getItmName, name).or().eq(TmplItmDO::getItmNum, code))
        ) > 0) {
            throw exception(TMPL_ITM_NAME_CODE_EXISTS);
        }
    }

    @Override
    @Transactional
    public boolean deleteTmplItm(List<Long> ids) {
        validateTmplLtmExists(ids);
        return removeBatchByIds(ids);
    }

    @Override
    public PageResult<TmplItmDO> pageTmplItm(TmpItmPageReqVO pageReqVO) {
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TmplItmDO> listTmplItmByIds(List<String> ids) {
        return baseMapper.selectByIds(ids);
    }

    @Override
    public void updateTmplTpSts(UpdateStsVO updateStsVO) {
        log.info("更新模板字段状态，参数：【{}】", updateStsVO);
        baseMapper.update(Wrappers.<TmplItmDO>lambdaUpdate().set(TmplItmDO::getSts, updateStsVO.getSts()).in(TmplItmDO::getId, updateStsVO.getIds()));
    }
}
