package com.mosty.base.manager.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.manager.mapper.TbJcglBqMapper;
import com.mosty.base.manager.mapper.TbJcglKhmxMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglBqService;
import com.mosty.base.manager.service.TbJcglKhmxService;
import com.mosty.base.model.dto.base.SysMessageInfo;
import com.mosty.base.model.dto.jcgl.TbJcglBqDto;
import com.mosty.base.model.entity.jcgl.TbJcglBq;
import com.mosty.base.model.entity.jcgl.TbJcglKhmx;
import com.mosty.base.model.query.jcgl.TbJcglBqQuery;
import com.mosty.base.model.query.jcgl.TbJcglKhmxQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.jcgl.TbJcglBqVo;
import com.mosty.base.model.vo.jcgl.TbJcglKhmxVo;
import com.mosty.base.utils.BeanConvertUtils;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.utils.PingYinUtil;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Locale;

/**
 * 考核模型
 */
@AllArgsConstructor
@Service
public class TbJcglKhmxServiceImpl extends ServiceImpl<TbJcglKhmxMapper, TbJcglKhmx> implements TbJcglKhmxService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public TbJcglKhmxVo queryById(String id) {
        return BeanConvertUtils.copyProperties(this.baseMapper.selectById(id), TbJcglKhmxVo.class);
    }

    /**
     * 分页查询
     *
     * @param tbJcglkhmx 筛选条件
     * @return
     */
    public IPage<TbJcglKhmx> paginQuery(TbJcglKhmxQuery tbJcglkhmx) {
        IPage<TbJcglKhmx> page = new Page<TbJcglKhmx>(tbJcglkhmx.getPageCurrent(), tbJcglkhmx.getPageSize());
        tbJcglkhmx.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(tbJcglkhmx.getSsbmdm(), null));
        LambdaQueryWrapper<TbJcglKhmx> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.isNotBlank(tbJcglkhmx.getSsbmdm()), TbJcglKhmx::getSsbmdm, tbJcglkhmx.getSsbmdm())
                .eq(StringUtils.isNotBlank(tbJcglkhmx.getMxmc()), TbJcglKhmx::getMxmc, tbJcglkhmx.getMxmc())
                .orderByDesc(TbJcglKhmx::getXtCjsj);
        return baseMapper.selectPage(page, wrapper);
    }

    /**
     * 新增数据
     *
     * @param tbJcglKhmx 实例对象
     * @return 实例对象
     */
    public int insert(TbJcglKhmx tbJcglKhmx) {

        tbJcglKhmx.setId(UUIDGenerator.getUUID());
        tbJcglKhmx.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(tbJcglKhmx.getSsbmdm());
        if (dept != null) {
            tbJcglKhmx.setSsbm(dept.getDeptname());
            tbJcglKhmx.setSsbmid(dept.getDeptid());
            tbJcglKhmx.setSsbmdm(dept.getDeptcode());
            tbJcglKhmx.setSsxgajdm(dept.getFxjcode());
            tbJcglKhmx.setSsxgajid(dept.getFxjid());
            tbJcglKhmx.setSsxgaj(dept.getFxjname());
            tbJcglKhmx.setSssgaj(dept.getDszname());
            tbJcglKhmx.setSssgajdm(dept.getDszcode());
            tbJcglKhmx.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.insert(tbJcglKhmx);

    }

    /**
     * 更新数据
     *
     * @param tbJcglKhmx 实例对象
     * @return 实例对象
     */
    public int update(TbJcglKhmx tbJcglKhmx) {
        if (StringUtils.isEmpty(tbJcglKhmx.getId())) throw new BusinessException("模型id为空！！");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(tbJcglKhmx.getSsbmdm());
        if (dept != null) {
            tbJcglKhmx.setSsbm(dept.getDeptname());
            tbJcglKhmx.setSsbmid(dept.getDeptid());
            tbJcglKhmx.setSsbmdm(dept.getDeptcode());
            tbJcglKhmx.setSsxgajdm(dept.getFxjcode());
            tbJcglKhmx.setSsxgajid(dept.getFxjid());
            tbJcglKhmx.setSsxgaj(dept.getFxjname());
            tbJcglKhmx.setSssgaj(dept.getDszname());
            tbJcglKhmx.setSssgajdm(dept.getDszcode());
            tbJcglKhmx.setSssgajid(dept.getDszid());
        }
        return baseMapper.updateById(tbJcglKhmx);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(String id) {
        int total = baseMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public boolean deleteByIds(List<String> id) {
        return baseMapper.deleteBatchIds(id) > 0;
    }

}