package com.dxhy.admin.sys.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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.admin.sys.dao.SysRulesDeptDao;
import com.dxhy.admin.sys.dao.SysSignRulesDao;
import com.dxhy.admin.sys.entity.SysRulesDeptEntity;
import com.dxhy.admin.sys.entity.SysSignRulesEntity;
import com.dxhy.admin.sys.service.SysRulesDeptService;
import com.dxhy.admin.sys.service.SysSignRulesService;
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;

/**
 * @author jiaohongyang
 */
@Service("sysSignRulesService")
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class SysSignRulesServiceImpl extends ServiceImpl<SysSignRulesDao, SysSignRulesEntity>
    implements SysSignRulesService {

    @Resource
    private SysRulesDeptService sysRulesDeptService;

    @Resource
    private SysRulesDeptDao sysRulesDeptDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String deptId = (String)params.get("deptId");
        String deptType = (String)params.get("deptType");
        List<Long> ruleIds = new ArrayList<>();
        IPage<SysSignRulesEntity> page = null;
        if (StringUtils.isNotBlank(deptId) && !"0".equals(deptType)) {

            List<SysRulesDeptEntity> ruleDepts =
                sysRulesDeptDao.selectList(new QueryWrapper<SysRulesDeptEntity>().eq("dept_id", deptId));

            if (ruleDepts != null && ruleDepts.size() > 0) {
                for (SysRulesDeptEntity entity : ruleDepts) {
                    ruleIds.add(entity.getRuleId());
                }
                page = this.page(new Query<SysSignRulesEntity>().getPage(params),
                    new QueryWrapper<SysSignRulesEntity>().in("rule_id", ruleIds));
            } else {
                page = this.page(new Query<SysSignRulesEntity>().getPage(params),
                    new QueryWrapper<SysSignRulesEntity>().eq("rule_id", null));
            }
        } else {
            page = this.page(new Query<SysSignRulesEntity>().getPage(params), new QueryWrapper<>());
        }

        return new PageUtils(page);

    }

    @Override
    public List<SysSignRulesEntity> unAllotList(Map<String, Object> params) {
        String deptId = (String)params.get("deptId");
        List<Long> ruleIds = new ArrayList<>();
        List<SysRulesDeptEntity> ruleDepts =
            sysRulesDeptDao.selectList(new QueryWrapper<SysRulesDeptEntity>().eq("dept_id", deptId));

        List<SysSignRulesEntity> list = new ArrayList<>();
        if (ruleDepts != null && ruleDepts.size() > 0) {
            for (SysRulesDeptEntity entity : ruleDepts) {
                ruleIds.add(entity.getRuleId());
            }
            list = this.baseMapper.selectList(new QueryWrapper<SysSignRulesEntity>().notIn("rule_id", ruleIds));
        } else {
            list = this.baseMapper.selectList(new QueryWrapper<>());
        }
        return list;
    }

    @Override
    public SysSignRulesEntity queryRules(String rulesCode) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);

        return this.baseMapper.selectOne(new QueryWrapper<SysSignRulesEntity>().eq("rule_code", rulesCode));
    }

    @Override
    public void saveRules(SysSignRulesEntity rulesEntity) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).saveSignRules(rulesEntity);
    }

    @Override
    public void updateRules(SysSignRulesEntity rulesEntity) {

        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateSignRules(rulesEntity);
    }

    @Override
    public void deleteRules(Long ruleId) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteRulesId(ruleId);
    }

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

    @Transactional(rollbackFor = Exception.class)
    public void updateSignRules(SysSignRulesEntity entity) {
        this.saveOrUpdate(entity);

    }

    @Transactional(rollbackFor = Exception.class)
    public void saveSignRules(SysSignRulesEntity entity) {
        this.baseMapper.insert(entity);

    }
}
