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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.tenant.core.aop.TenantIgnore;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplInscDatBsnPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplInscDatBsnRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplInscDatBsnSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplInscDatBsnDO;
import com.zt.plat.module.base.dal.mysql.tmpltp.TmplInscDatBsnMapper;
import jakarta.annotation.Resource;
import jodd.util.ArraysUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
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_INSC_DAT_BSN_EXISTS;
import static com.zt.plat.module.tmpltp.enums.ErrorCodeConstants.TMPL_INSC_DAT_BSN_NOT_EXISTS;

/**
 * 业务实例字段值 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class TmplInscDatBsnServiceImpl implements TmplInscDatBsnService {

    @Resource
    private TmplInscDatBsnMapper tmplInscDatBsnMapper;

    @Override
    public TmplInscDatBsnRespVO createTmplInscDatBsn(TmplInscDatBsnSaveReqVO createReqVO) {
        //判断存在
        validateTmplInscDatBsnExistsByKey(createReqVO);
        // 插入
        createReqVO.setFldVal(null);// 兼容前端传入数组或者字符串
        TmplInscDatBsnDO tmplInscDatBsn = BeanUtils.toBean(createReqVO, TmplInscDatBsnDO.class);
        tmplInscDatBsnMapper.insert(tmplInscDatBsn);
        // 返回
        TmplInscDatBsnRespVO tmplInscDatBsnRespVO = BeanUtils.toBean(tmplInscDatBsn, TmplInscDatBsnRespVO.class);
        tmplInscDatBsnRespVO.setFldVal(CollUtil.toList(tmplInscDatBsn.getFldVal().split(",")));
        return tmplInscDatBsnRespVO;
    }
    private void validateTmplInscDatBsnExistsByKey(TmplInscDatBsnSaveReqVO createReqVO) {
        List<TmplInscDatBsnDO> tmplInscDatBsnDOS = tmplInscDatBsnMapper.selectList(TmplInscDatBsnDO::getInscFldId, createReqVO.getInscFldId(),TmplInscDatBsnDO::getBsnId, createReqVO.getBsnId());
        if (!tmplInscDatBsnDOS.isEmpty()) {
           throw exception(TMPL_INSC_DAT_BSN_EXISTS);
       }
    }

    @Override
    public void updateTmplInscDatBsn(TmplInscDatBsnSaveReqVO updateReqVO) {
        // 校验存在
       // validateTmplInscDatBsnExists(updateReqVO.getId());
        // 物理删除旧记录
        tmplInscDatBsnMapper.physicalDeleteByBsnIds(List.of(updateReqVO.getBsnId()));

        TmplInscDatBsnDO saveObj = BeanUtils.toBean(updateReqVO, TmplInscDatBsnDO.class);
        saveObj.setId(null);
        tmplInscDatBsnMapper.insert(saveObj);
    }

    @Override
    public void updateTmplInscDatBsnBatch(List<TmplInscDatBsnSaveReqVO> updateReqVOS) {
        tmplInscDatBsnMapper.physicalDeleteByBsnIds(updateReqVOS.stream().map(TmplInscDatBsnSaveReqVO::getBsnId).toList());
        List<TmplInscDatBsnDO> bean = BeanUtils.toBean(updateReqVOS, TmplInscDatBsnDO.class);
        tmplInscDatBsnMapper.insertBatch(bean);
    }

    @Override
    public void deleteTmplInscDatBsn(String id) {
        // 校验存在
        validateTmplInscDatBsnExists(id);
        // 删除
        tmplInscDatBsnMapper.deleteById(id);
    }

    @Override
        public void deleteTmplInscDatBsnListByIds(List<String> ids) {
        // 校验存在
        validateTmplInscDatBsnExists(ids);
        // 删除
        tmplInscDatBsnMapper.deleteByIds(ids);
        }

    private void validateTmplInscDatBsnExists(List<String> ids) {
        List<TmplInscDatBsnDO> list = tmplInscDatBsnMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TMPL_INSC_DAT_BSN_NOT_EXISTS);
        }
    }

    private void validateTmplInscDatBsnExists(String id) {
        if (tmplInscDatBsnMapper.selectById(id) == null) {
            throw exception(TMPL_INSC_DAT_BSN_NOT_EXISTS);
        }
    }

    @Override
    public TmplInscDatBsnDO getTmplInscDatBsn(String id) {
        return tmplInscDatBsnMapper.selectById(id);
    }

    @Override
    public PageResult<TmplInscDatBsnDO> getTmplInscDatBsnPage(TmplInscDatBsnPageReqVO pageReqVO) {
        return tmplInscDatBsnMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    public void deleteTmplInscDatBsnListByBsnIds(List<String> ids) {
         tmplInscDatBsnMapper.update(new LambdaUpdateWrapper<TmplInscDatBsnDO>().in(TmplInscDatBsnDO::getBsnId, ids).set(TmplInscDatBsnDO::getDeleted, 1));
    }

    @Override
    public List<TmplInscDatBsnRespVO> getTmplInscDatBsnListByBsnId(String id) {
        List<TmplInscDatBsnDO> tmplInscDatBsnDOS = tmplInscDatBsnMapper.selectList(TmplInscDatBsnDO::getBsnId, id);
        return BeanUtils.toBean(tmplInscDatBsnDOS, TmplInscDatBsnRespVO.class);
    }

}
