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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplFldPageReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplFldRespVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.TmplTpFldSaveReqVO;
import com.zt.plat.module.base.controller.admin.templtp.vo.UpdateStsVO;
import com.zt.plat.module.base.dal.dataobject.tmpltp.TmplTpFldDO;
import com.zt.plat.module.base.dal.mysql.tmpltp.TmplTpFldMapper;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.plat.module.base.util.ClassInfoScanner;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


@Service
@Validated
@Slf4j
public class TmplTpFldServiceImpl extends ServiceImpl<TmplTpFldMapper, TmplTpFldDO> implements TmplTpFldService {
    @Override
    public TmplFldRespVO createTmplFld(TmplTpFldSaveReqVO tmplTpFldSaveReqVO) {
        TmplTpFldDO tmplTpFldDO = BeanUtils.toBean(tmplTpFldSaveReqVO, TmplTpFldDO.class);
        validateTmplFldCodeExists(tmplTpFldSaveReqVO.getFldKy());
        tmplTpFldDO.setSts(1);
        baseMapper.insert(tmplTpFldDO);
        log.info("创建模板字段成功，模板字段信息：【{}】", tmplTpFldDO);
        return BeanUtils.toBean(tmplTpFldDO, TmplFldRespVO.class);
    }

    @Override
    public void updateTmplFld(TmplTpFldSaveReqVO tmplTpFldSaveReqVO) {
        // 校验存在
        validateTmplFldExists(tmplTpFldSaveReqVO.getId());
        // 更新
        TmplTpFldDO updateObj = BeanUtils.toBean(tmplTpFldSaveReqVO, TmplTpFldDO.class);
        baseMapper.updateById(updateObj);
        log.info("更新模板字段成功，模板字段信息：【{}】", updateObj);
    }

    private void  validateTmplFldCodeExists(String code){
        Long loginUserCompanyId = SecurityFrameworkUtils.getLoginUserCompanyId();
        if (baseMapper.selectCount(Wrappers.<TmplTpFldDO>lambdaQuery().eq(TmplTpFldDO::getFldKy, code).eq(TmplTpFldDO::getCompanyId, loginUserCompanyId)) > 0) {
            throw exception(TMPL_FLD_CODE_EXISTS);
        }
    }

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

    private void validateTmplFldExists(Long id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(TMPL_FLD_NOT_EXISTS);
        }
    }

    @Override
    public PageResult<TmplTpFldDO> tmplTpFldPage(TmplFldPageReqVO pageReqVO) {
        log.info("获取模板字段列表，参数：【{}】", pageReqVO);
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteTmplTpByIds(List<Long> ids) {
        validateTmplFldExists(ids);
        baseMapper.deleteByIds(ids);
        log.info("删除模板字段成功，模板字段IDS：【{}】", ids);
    }

    @Override
    public List<Map<String, Object>> getTmplTpListByClass(String clazz) {
        log.info("获取模板字段列表，模板字段类：【{}】", clazz);
        return ClassInfoScanner.getClassFieldInfo(clazz);
    }

    @Override
    public List<TmplTpFldDO> getTmplTpListByValKeys(List<String> valNames) {
        if (CollUtil.isEmpty(valNames)){
            return new ArrayList<>();
        }
        return baseMapper.selectList(new LambdaQueryWrapper<TmplTpFldDO>().in(TmplTpFldDO::getFldKy, valNames));
    }

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