package com.xunmei.core.access.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.constant.SecurityConstants;
import com.xunmei.common.core.domain.R;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.core.utils.StringUtils;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.core.access.domain.TMonitoringRetrievalPlan;
import com.xunmei.core.access.mapper.TMonitoringRetrievalPlanMapper;
import com.xunmei.core.access.service.ITMonitoringRetrievalPlanService;
import com.xunmei.system.api.RemoteOrgService;
import com.xunmei.system.api.RemoteRoleService;
import com.xunmei.system.api.RemoteUserService;
import com.xunmei.system.api.domain.SysOrg;
import com.xunmei.system.api.domain.SysRole;
import com.xunmei.system.api.domain.SysUser;
import com.xunmei.system.api.function.RemoteCallHandlerExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * 监控调阅计划Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-24
 */
@Service
public class TMonitoringRetrievalPlanServiceImpl extends ServiceImpl<TMonitoringRetrievalPlanMapper, TMonitoringRetrievalPlan> implements ITMonitoringRetrievalPlanService {
    @Autowired
    private TMonitoringRetrievalPlanMapper tMonitoringRetrievalPlanMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteOrgService remoteOrgService;
    @Autowired
    private RemoteRoleService remoteRoleService;
private static final Long MINUS_ONE=-1L;
private static final Long ZERO=0L;
    @Override
    public TableDataInfo<TMonitoringRetrievalPlan> selectPage(TMonitoringRetrievalPlan tMonitoringRetrievalPlan) {
        //未删除
        tMonitoringRetrievalPlan.setIsDeleted(ZERO);
        Page<TMonitoringRetrievalPlan> page;
        //分页
        if (tMonitoringRetrievalPlan.getPageNum() != null && tMonitoringRetrievalPlan.getPageSize() != null) {
            page = new Page<>(tMonitoringRetrievalPlan.getPageNum(), tMonitoringRetrievalPlan.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        String planName = tMonitoringRetrievalPlan.getPlanName();
        tMonitoringRetrievalPlan.setPlanName(null);

        QueryWrapper<TMonitoringRetrievalPlan> query = new QueryWrapper<>(tMonitoringRetrievalPlan);
        //下穿
        tMonitoringRetrievalPlan.setCheckSub(true);
        List<SysOrg> data = remoteOrgService.selectSysOrgByParentId(-MINUS_ONE, SecurityConstants.INNER);
        if (null == tMonitoringRetrievalPlan.getOrgId()) {
            tMonitoringRetrievalPlan.setOrgId(data.get(0).getId());
        }
        if (tMonitoringRetrievalPlan.getCheckSub()) {
            //List<Long> ids = remoteOrgService.selectCheckSubOrgIdList(tMonitoringRetrievalPlan.getOrgId(), SecurityConstants.INNER);
            List<Long> ids = remoteOrgService.selectCheckSubOrgIdList(tMonitoringRetrievalPlan.getOrgId(), SecurityConstants.INNER);
            //清空前端传递的org_id
            tMonitoringRetrievalPlan.setOrgId(null);
            //添加in条件
            query.in("org_id", ids);
        }
        if (StringUtils.isNotEmpty(planName)) {
            query.like("plan_name", planName);
        }
        //时间范围查询
        if (tMonitoringRetrievalPlan.getParams().get("beginTime") != null && tMonitoringRetrievalPlan.getParams().get("endTime") != null) {
            query.between("create_time", tMonitoringRetrievalPlan.getParams().get("beginTime"), tMonitoringRetrievalPlan.getParams().get("endTime"));
        }
        query.orderByDesc("create_time");
        //获取数据
        page = tMonitoringRetrievalPlanMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);


    }


    /**
     * 查询监控调阅计划
     *
     * @param id 监控调阅计划主键
     * @return 监控调阅计划
     */
    @Override
    public TMonitoringRetrievalPlan selectTMonitoringRetrievalPlanById(Long id) {
        return tMonitoringRetrievalPlanMapper.selectById(id);
    }

    @Override
    public int distribute(Long id) {
        //查询
        TMonitoringRetrievalPlan tMonitoringRetrievalPlan = tMonitoringRetrievalPlanMapper.selectById(id);
        tMonitoringRetrievalPlan.setPlanStatus(0);
        tMonitoringRetrievalPlanMapper.updateById(tMonitoringRetrievalPlan);
        List<SysOrg> list = remoteOrgService.selectSysOrgByParentId(-MINUS_ONE, SecurityConstants.INNER);
        Integer i = 0;
        for (SysOrg l : list) {
            TMonitoringRetrievalPlan monitoringRetrievalPlan = new TMonitoringRetrievalPlan();
            monitoringRetrievalPlan.setPlanName(tMonitoringRetrievalPlan.getPlanName());
            monitoringRetrievalPlan.setOrgId(l.getId());
            monitoringRetrievalPlan.setOrgName(l.getName());
            monitoringRetrievalPlan.setRoleId(tMonitoringRetrievalPlan.getRoleId());
            monitoringRetrievalPlan.setRoleName(tMonitoringRetrievalPlan.getRoleName());
            monitoringRetrievalPlan.setOrgPath(l.getPath());
            monitoringRetrievalPlan.setOrgType("6");
            monitoringRetrievalPlan.setRoleId(tMonitoringRetrievalPlan.getRoleId());
            monitoringRetrievalPlan.setPlanCycle(tMonitoringRetrievalPlan.getPlanCycle());
            monitoringRetrievalPlan.setPlanFrequency(tMonitoringRetrievalPlan.getPlanFrequency());
            monitoringRetrievalPlan.setPlanStatus(0);
            monitoringRetrievalPlan.setCreateBy(SecurityUtils.getUsername());
            monitoringRetrievalPlan.setUpdateTime(DateUtils.getNowDate());
            monitoringRetrievalPlan.setUpdateBy(SecurityUtils.getUsername());
            monitoringRetrievalPlan.setCreateTime(DateUtils.getNowDate());
            monitoringRetrievalPlan.setDescription(tMonitoringRetrievalPlan.getDescription());
            monitoringRetrievalPlan.setCreateType(1);
            i = tMonitoringRetrievalPlanMapper.insert(monitoringRetrievalPlan);
            if (i < 0) {
                return i;
            }
        }
        return i;
    }

    /**
     * 查询监控调阅计划列表
     *
     * @param tMonitoringRetrievalPlan 监控调阅计划
     * @return 监控调阅计划
     */
    @Override
    public List<TMonitoringRetrievalPlan> selectTMonitoringRetrievalPlanList(TMonitoringRetrievalPlan tMonitoringRetrievalPlan) {
        return tMonitoringRetrievalPlanMapper.selectList(new QueryWrapper<>(tMonitoringRetrievalPlan));
    }

    /**
     * 新增监控调阅计划
     *
     * @param tMonitoringRetrievalPlan 监控调阅计划
     * @return 结果
     */
    @Override
    public int insertTMonitoringRetrievalPlan(TMonitoringRetrievalPlan tMonitoringRetrievalPlan) {
        tMonitoringRetrievalPlan.setCreateTime(DateUtils.getNowDate());
        SysUser sysUser = remoteUserService.getUserById(SecurityUtils.getUserId(), SecurityConstants.INNER);
        SysOrg data = remoteOrgService.selectSysOrgById(sysUser.getOrgId(), SecurityConstants.INNER);
        tMonitoringRetrievalPlan.setOrgName(data.getName());
        tMonitoringRetrievalPlan.setOrgId(sysUser.getOrgId());
        tMonitoringRetrievalPlan.setCreateBy(SecurityUtils.getUsername());
        tMonitoringRetrievalPlan.setUpdateTime(DateUtils.getNowDate());
        tMonitoringRetrievalPlan.setUpdateBy(SecurityUtils.getUsername());
        SysRole sysRole = remoteRoleService.getRoleById(tMonitoringRetrievalPlan.getRoleId(), SecurityConstants.INNER);
        tMonitoringRetrievalPlan.setRoleName(sysRole.getRoleName());
        if (tMonitoringRetrievalPlan.getOrgType().equals("1")) {
            tMonitoringRetrievalPlan.setPlanStatus(1);
        } else {
            tMonitoringRetrievalPlan.setPlanStatus(0);
        }
        tMonitoringRetrievalPlan.setCreateType(0);
        return tMonitoringRetrievalPlanMapper.insert(tMonitoringRetrievalPlan);
    }

    /**
     * 修改监控调阅计划
     *
     * @param tMonitoringRetrievalPlan 监控调阅计划
     * @return 结果
     */
    @Override
    public int updateTMonitoringRetrievalPlan(TMonitoringRetrievalPlan tMonitoringRetrievalPlan) {
        tMonitoringRetrievalPlan.setUpdateTime(DateUtils.getNowDate());
        return tMonitoringRetrievalPlanMapper.updateById(tMonitoringRetrievalPlan);
    }

    /**
     * 批量删除监控调阅计划(逻辑删除
     *
     * @param ids 需要删除的监控调阅计划主键
     * @return 结果
     */
    @Override
    public int deleteTMonitoringRetrievalPlanByIds(Long[] ids) {
        int i = 0;
        for (Long id : ids) {
            TMonitoringRetrievalPlan tMonitoringRetrievalPlan = new TMonitoringRetrievalPlan();
            tMonitoringRetrievalPlan.setIsDeleted(2L);
            tMonitoringRetrievalPlan.setId(id);
            i = tMonitoringRetrievalPlanMapper.updateTMonitoringRetrievalPlan(tMonitoringRetrievalPlan);
            if (i < 1) {
                return i;
            }
        }
        return i;
    }

    /**
     * 删除监控调阅计划信息
     *
     * @param id 监控调阅计划主键
     * @return 结果
     */
    @Override
    public int deleteTMonitoringRetrievalPlanById(Long id) {
        return tMonitoringRetrievalPlanMapper.deleteById(id);
    }

    @Override
    public boolean saveBatch(Collection<TMonitoringRetrievalPlan> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<TMonitoringRetrievalPlan> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<TMonitoringRetrievalPlan> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(TMonitoringRetrievalPlan entity) {
        return false;
    }

    @Override
    public TMonitoringRetrievalPlan getOne(Wrapper<TMonitoringRetrievalPlan> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<TMonitoringRetrievalPlan> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<TMonitoringRetrievalPlan> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }
}
