package com.dxhy.jxpt.baseService.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.api.model.config.sys.SysDicttypeEntity;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;
import com.dxhy.jxpt.baseService.config.dao.SysDicttypeDao;
import com.dxhy.jxpt.baseService.config.service.SysDicttypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author jiaohongyang
 */
@Service("sysDicttypeService")
@Slf4j
public class SysDicttypeServiceImpl extends ServiceImpl<SysDicttypeDao, SysDicttypeEntity>
    implements SysDicttypeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String dicttypename = (String)params.get("dicttypename");
        String dicttypecode = (String)params.get("dicttypecode");

        IPage<SysDicttypeEntity> page = this.page(new Query<SysDicttypeEntity>().getPage(params),
            new QueryWrapper<SysDicttypeEntity>()
                .like(StringUtils.isNotBlank(dicttypename), "dicttypename", dicttypename)
                .like(StringUtils.isNotBlank(dicttypecode), "dicttypecode", dicttypecode));

        return new PageUtils(page);
    }

    @Override
    public Integer queryCode(String code, Long dictTypeId) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return Math.toIntExact(baseMapper.selectCount(new QueryWrapper<SysDicttypeEntity>().eq("dicttypecode", code)
                .ne(dictTypeId != null, "dicttypeid", dictTypeId)));
    }

    @Override
    public SysDicttypeEntity queryDicttypeById(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);

        return baseMapper.selectById(id);
    }

    @Override
    public void saveDicttype(SysDicttypeEntity dict) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).insertType(dict);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertType(SysDicttypeEntity dict) {
        baseMapper.insert(dict);
    }

    @Override
    public void updateDictType(SysDicttypeEntity dict) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateType(dict);

    }

    @Transactional(rollbackFor = Exception.class)
    public void updateType(SysDicttypeEntity dict) {
        baseMapper.updateById(dict);
    }

    @Override
    public void deleteDictType(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteType(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteType(Long id) {
        baseMapper.deleteById(id);
    }

}
