package com.dcsoft.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.system.domain.SysBranch;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.SysSyncItem;
import com.dcsoft.system.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcsoft.system.mapper.SysSyncRuleMapper;
import com.dcsoft.system.domain.SysSyncRule;
import com.dcsoft.system.service.ISysSyncRuleService;

/**
 * 同步规则Service业务层处理
 *
 * @author nichun
 * @date 2024-01-11
 */
@Service
public class SysSyncRuleServiceImpl implements ISysSyncRuleService
{
    @Autowired
    private SysSyncRuleMapper sysSyncRuleMapper;

    /**
     * 查询同步规则
     *
     * @param id 同步规则主键
     * @return 同步规则
     */
    @Override
    public SysSyncRule selectSysSyncRuleById(Long id)
    {
        return sysSyncRuleMapper.selectSysSyncRuleById(id);
    }

    /**
     * 查询同步规则列表
     *
     * @param sysSyncRule 同步规则
     * @return 同步规则
     */
    @Override
    public List<SysSyncRule> selectSysSyncRuleList(SysSyncRule sysSyncRule)
    {
        return sysSyncRuleMapper.selectSysSyncRuleList(sysSyncRule);
    }

    /**
     * 新增同步规则
     *
     * @param sysSyncRule 同步规则
     * @return 结果
     */
    @Override
    public int insertSysSyncRule(SysSyncRule sysSyncRule)
    {
        sysSyncRule.setCreateTime(DateUtils.getNowDate());
        return sysSyncRuleMapper.insertSysSyncRule(sysSyncRule);
    }

    /**
     * 修改同步规则
     *
     * @param sysSyncRule 同步规则
     * @return 结果
     */
    @Override
    public int updateSysSyncRule(SysSyncRule sysSyncRule)
    {
        sysSyncRule.setUpdateTime(DateUtils.getNowDate());
        return sysSyncRuleMapper.updateSysSyncRule(sysSyncRule);
    }

    /**
     * 批量删除同步规则
     *
     * @param ids 需要删除的同步规则主键
     * @return 结果
     */
    @Override
    public int deleteSysSyncRuleByIds(Long[] ids)
    {
        return sysSyncRuleMapper.deleteSysSyncRuleByIds(ids);
    }

    /**
     * 删除同步规则信息
     *
     * @param id 同步规则主键
     * @return 结果
     */
    @Override
    public int deleteSysSyncRuleById(Long id)
    {
        return sysSyncRuleMapper.deleteSysSyncRuleById(id);
    }

    @Override
    public int insetSysSyncRuleItem(SysSyncItem item) {
        return sysSyncRuleMapper.insetSysSyncRuleItem(item);
    }

    @Override
    public int deleteSysSyncRuleItem(Long id) {
        return sysSyncRuleMapper.deleteSysSyncRuleItem(id);
    }

    @Override
    public List<SysSyncItem> selectSysSyncRuleItem(Long id) {
        return sysSyncRuleMapper.selectSysSyncRuleItem(id);
    }

    @Override
    public String addDept(SysBranch branch, String flag) {
        String msg="";
        //判断规则部门，推送到相应增量规则上
        SysSyncRule rule=new SysSyncRule();
        rule.setBranchId(branch.getParentId());
        List<SysSyncRule> itemList=sysSyncRuleMapper.selectSysSyncItemRuleList(rule);
        for(SysSyncRule item:itemList){
            JSONObject paramMap=new JSONObject();
            paramMap.put("sysBranch",branch);
            paramMap.put("flag",flag);
            msg= HttpUtil.postDataJson(item.getAddDeptUrl(),paramMap);
        }
        return msg;
    }

    @Override
    public String addPeople(SysPeople sysPeople, String flag) {
        String msg="";
        //判断规则部门，推送到相应增量规则上
        SysSyncRule rule=new SysSyncRule();
        rule.setBranchId(sysPeople.getBranchId());
        List<SysSyncRule> itemList=sysSyncRuleMapper.selectSysSyncItemRuleList(rule);
        for(SysSyncRule item:itemList){
            JSONObject paramMap=new JSONObject();
            paramMap.put("sysPeople",sysPeople);
            paramMap.put("flag",flag);
            msg= HttpUtil.postDataJson(item.getAddPeopleUrl(),paramMap);
        }
        return msg;
    }

    /**
     * 查询园区对映的部门
     * @param tdGuid
     * @return
     */
    @Override
    public List<SysSyncItem> queryParkDept(String tdGuid) {
        return sysSyncRuleMapper.queryParkDept(tdGuid);
    }
}
