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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.jcgl.TbJcglBqDto;
import com.mosty.base.model.entity.jcgl.TbJcglBq;
import com.mosty.base.model.query.jcgl.TbJcglBqQuery;
import com.mosty.base.utils.BeanConvertUtils;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.utils.PingYinUtil;
import com.mosty.base.model.vo.jcgl.TbJcglBqVo;
import com.mosty.base.manager.mapper.TbJcglBqMapper;
import com.mosty.base.manager.service.TbJcglBqService;
import com.mosty.common.base.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

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

/**
 * 巡防警力标签表（技能/经验）;(tb_jcgl_bq)表服务实现类
 * @author : http://www.chiner.pro
 * @date : 2022-6-6
 */
@AllArgsConstructor
@Service
public class TbJcglBqServiceImpl extends ServiceImpl<TbJcglBqMapper, TbJcglBq> implements TbJcglBqService {

    private final TbJcglBqMapper tbJcglBqMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public TbJcglBqVo queryById(String id){
        return BeanConvertUtils.copyProperties(tbJcglBqMapper.selectById(id), TbJcglBqVo.class);
    }
    
    /**
     * 分页查询
     *
     * @param tbJcglBq 筛选条件
     * @return
     */
    public IPage<TbJcglBqVo> paginQuery(TbJcglBqQuery tbJcglBq){
        IPage<TbJcglBq> page = PageUtils.buildPage(tbJcglBq.getPageSize(),tbJcglBq.getPageCurrent());
        return tbJcglBqMapper.paginQuery(page, tbJcglBq);
//        return tbJcglBqIPage.convert(TbJcglBq -> BeanConvertUtils.copyProperties(TbJcglBq, TbJcglBqVo.class));
    }

    private LambdaQueryWrapper<TbJcglBq> queryWrapper(TbJcglBqQuery tbJcglBq){
        LambdaQueryWrapper<TbJcglBq> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(tbJcglBq.getBqlx())){
            queryWrapper.eq(TbJcglBq::getBqlx, tbJcglBq.getBqlx());
        }
        if(StrUtil.isNotBlank(tbJcglBq.getBqmc())){
            queryWrapper.like(TbJcglBq::getBqmc, tbJcglBq.getBqmc());
        }
        if(StrUtil.isNotBlank(tbJcglBq.getBqpym())){
            queryWrapper.eq(TbJcglBq::getBqpym, tbJcglBq.getBqpym());
        }
        return queryWrapper;
    }
    
    /**
     * 新增数据
     *
     * @param tbJcglBq 实例对象
     * @return 实例对象
     */
    public int insert(TbJcglBqDto tbJcglBq){
        try{
            if(checkMc(tbJcglBq.getBqmc())){
                throw new BusinessException("标签名称已存在");
            }
//            String bqbh = "JN0001";
            TbJcglBq insert = BeanConvertUtils.copyProperties(tbJcglBq, TbJcglBq.class);
            insert.setXtSjly("1");
            insert.setBqbh(tbJcglBq.getBqbh());
            insert.setBqpym(PingYinUtil.getFirstSpell(insert.getBqmc()).toUpperCase(Locale.ROOT));
            return tbJcglBqMapper.insert(insert);
        }catch (Exception e){
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public boolean checkMc(String mc){
        LambdaQueryWrapper<TbJcglBq> queryWrapper = new LambdaQueryWrapper<>();
        Integer integer = tbJcglBqMapper.selectCount(queryWrapper.eq(TbJcglBq::getBqmc, mc));
        if(integer > 0 ){
            return true;
        }
        return false;
    }

    @Override
    public Object countBqsl(TbJcglBqVo tbJcglBqVo) {
        List<TbJcglBqVo> tbJcglBq =null;
        try {
             tbJcglBq = tbJcglBqMapper.countBqsl(tbJcglBqVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return tbJcglBq;
    }

    /**
     * 更新数据
     *
     * @param tbJcglBq 实例对象
     * @return 实例对象
     */
    public int update(TbJcglBqDto tbJcglBq){
        TbJcglBq update = BeanConvertUtils.copyProperties(tbJcglBq, TbJcglBq.class);
        update.setBqpym(PingYinUtil.getFirstSpell(update.getBqmc()).toUpperCase(Locale.ROOT));
        return tbJcglBqMapper.updateById(update);
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(String id){
        int total = tbJcglBqMapper.deleteById(id);
        return total > 0;
    }

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

    @Override
    public boolean zcbqByIds(List<String> id) {
        return tbJcglBqMapper.zxBatch(id) > 0;
    }

    //自动生成编号暂时保留
    //            int bqsl = tbJcglBqMapper.countBqslByBqlx(tbJcglBq.getBqlx());
//            if(bqsl > 0) {
//                bqsl = bqsl + 1;
//                if(bqsl < 10) {
//                    bqbh ="JN000" + bqsl;
//                } else if(bqsl >= 10 && bqsl < 100) {
//                    bqbh ="JN00" + bqsl;
//                } else if(bqsl >= 100 && bqsl < 1000) {
//                    bqbh ="JN0" + bqsl;
//                } else {
//                    bqbh ="JN" + bqsl;
//                }
//            }

}