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.SysRulesDeptEntity;
import com.dxhy.api.model.config.sys.SysSignRulesEntity;
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.SysRulesDeptDao;
import com.dxhy.jxpt.baseService.config.dao.SysSignRulesDao;
import com.dxhy.jxpt.baseService.config.service.SysRulesDeptService;
import com.dxhy.jxpt.baseService.config.service.SysSignRulesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @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 groupId = (String)params.get("groupId");
        String deptType = (String)params.get("deptType");
        List<Long> ruleIds = new ArrayList<>();
        IPage<SysSignRulesEntity> page = null;
        if (StringUtils.isNotBlank(groupId) && !"0".equals(deptType)) {

            List<SysRulesDeptEntity> ruleDepts =
                sysRulesDeptDao.selectList(new QueryWrapper<SysRulesDeptEntity>()
                        .eq("group_id", Long.valueOf(groupId))
                );

            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<>());
        }*/
        String company = (String)params.get("company");
        List<Long> ruleIds = new ArrayList<>();
        IPage<SysSignRulesEntity> page = null;
        if (StringUtils.isNotBlank(company)) {

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

            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<>());
        }

        //IPage<SysSignRulesEntity> page = this.page(new Query<SysSignRulesEntity>().getPage(params), new QueryWrapper<SysSignRulesEntity>().eq("company", params.get("company").toString()));

        return new PageUtils(page);

    }

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

        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);

    }
}
