package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ActSuppliesRoleDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActSuppliesRoleMapper;
import com.ruoyi.system.service.IActSuppliesRoleService;

/**
 * 导入规则Service业务层处理
 * 
 * @author dongjg
 * @date 2023-07-12
 */
@Service
public class ActSuppliesRoleServiceImpl implements IActSuppliesRoleService 
{
    @Autowired
    private ActSuppliesRoleMapper actSuppliesRoleMapper;

    @Autowired
    private ActSuppliesRoleDetailMapper actSuppliesRoleDetailMapper;

    /**
     * 查询导入规则
     * 
     * @param id 导入规则ID
     * @return 导入规则
     */
    @Override
    public ActSuppliesRole selectActSuppliesRoleById(Integer id)
    {
        return actSuppliesRoleMapper.selectActSuppliesRoleById(id);
    }

    /**
     * 查询导入规则列表
     * 
     * @param actSuppliesRole 导入规则
     * @return 导入规则
     */
    @Override
    public List<ActSuppliesRole> selectActSuppliesRoleList(ActSuppliesRole actSuppliesRole)
    {
        return actSuppliesRoleMapper.selectActSuppliesRoleList(actSuppliesRole);
    }
    /**
     * 查询规则详情
     *
     * @param id 规则详情ID
     * @return 规则详情
     */
    @Override
    public ActSuppliesRoleDetail selectActSuppliesRoleDetailById(Integer id)
    {
        return actSuppliesRoleMapper.selectActSuppliesRoleDetailById(id);
    }

    /**
     * 查询规则详情列表
     *
     * @param id 规则详情
     * @return 规则详情
     */
    @Override
    public List<ActSuppliesRoleDetail> selectActSuppliesRoleDetailList(Integer id)
    {
        return actSuppliesRoleMapper.selectActSuppliesRoleDetailList(id);
    }


    /**
     * 新增导入规则
     * 
     * @param actSuppliesRole 导入规则
     * @return 结果
     */
    @Override
    public int insertActSuppliesRole(ActSuppliesRole actSuppliesRole)
    {
        int countByRoleName = actSuppliesRoleMapper.countByRoleName(actSuppliesRole);
        if(countByRoleName > 0){
            return 0;
        }

        actSuppliesRole.setCreateTime(DateUtils.getNowDate());

        return actSuppliesRoleMapper.insertActSuppliesRole(actSuppliesRole);
    }


    /**
     * 修改导入规则
     * 
     * @param actSuppliesRole 导入规则
     * @return 结果
     */
    @Override
    public int updateActSuppliesRole(ActSuppliesRole actSuppliesRole)
    {

        int countByRoleName = actSuppliesRoleMapper.countByRoleName(actSuppliesRole);
        if(countByRoleName > 0){
            return 0;
        }
        actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
        return actSuppliesRoleMapper.updateActSuppliesRole(actSuppliesRole);
    }

    @Override
    public int updateActSuppliesRoleDetail(ActSuppliesRoleDetail actSuppliesRoleDetail) {
        List<String> filteredList = new ArrayList<>();

        for (int i =0 ;i<actSuppliesRoleDetail.getDetailMH().length; i++){

            filteredList.add("{\"se\":\"0\",\"re\":\""+(Integer.parseInt((actSuppliesRoleDetail.getDetailYH()[i]))-1)+"\",\"ce\":\""+(Integer.parseInt((actSuppliesRoleDetail.getDetailYL()[i]))-1)+"\",\"st\":\"0\",\"rt\":\""+(Integer.parseInt((actSuppliesRoleDetail.getDetailMH()[i]))-1)+"\",\"ct\":\""+(Integer.parseInt((actSuppliesRoleDetail.getDetailML()[i]))-1)+"\",\"ys\":\""+actSuppliesRoleDetail.getConvertStatus()+"\"}") ;

        }

        String content = String.join(",",filteredList);

        String content2 = "["+content+"]";

        actSuppliesRoleDetail.setDetailContent(content2);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesRoleDetail.setUpdateBy(user.getUserName());
        actSuppliesRoleDetail.setUpdateTime(DateUtils.getNowDate());
        return actSuppliesRoleMapper.updateActSuppliesRoleDetail(actSuppliesRoleDetail);
    }

    /**
     * 批量删除导入规则
     * 
     * @param ids 需要删除的导入规则ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesRoleByIds(Long[] ids)
    {

        ActSuppliesRole actSuppliesRole = new ActSuppliesRole();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ActSuppliesRoleDetail actSuppliesRoleDetail = new ActSuppliesRoleDetail();

        for (int i =0;i<ids.length;i++){

            int i1 = actSuppliesRoleMapper.saveHistory(ids[0]);
            if (i1>0){
                actSuppliesRole.setId(ids[i]);
                actSuppliesRole.setStatus(2);
                actSuppliesRole.setUpdateBy(user.getUserName());
                actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleMapper.updateRelease(actSuppliesRole);

                actSuppliesRoleDetail.setRoleId(ids[i].intValue());
                actSuppliesRoleDetail.setStatus(2);
                actSuppliesRoleDetail.setUpdateBy(user.getUserName());
                actSuppliesRoleDetail.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleDetailMapper.updateRoleDetailStatus(actSuppliesRoleDetail);
            }else {

                ActSuppliesRole actSuppliesRole1 = actSuppliesRoleMapper.selectActSuppliesRoleById(ids[i].intValue());
                if (actSuppliesRole1.getStatus() == 0){
                    actSuppliesRole.setId(ids[i]);
                    actSuppliesRole.setStatus(1);
                    actSuppliesRole.setUpdateBy(user.getUserName());
                    actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
                    actSuppliesRoleMapper.updateRelease(actSuppliesRole);

                    actSuppliesRoleDetail.setRoleId(ids[i].intValue());
                    actSuppliesRoleDetail.setStatus(1);
                    actSuppliesRoleDetail.setUpdateBy(user.getUserName());
                    actSuppliesRoleDetail.setUpdateTime(DateUtils.getNowDate());
                    actSuppliesRoleDetailMapper.updateRoleDetailStatus(actSuppliesRoleDetail);
                }else {
                    actSuppliesRoleMapper.deleteActSuppliesRoleById(ids[i].intValue());
                    actSuppliesRoleDetailMapper.deleteActSuppliesRoleDetailById(ids[i].intValue());
                }

            }

        }
        return 1;
    }

    /**
     * 删除导入规则信息
     * 
     * @param id 导入规则ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesRoleById(Integer id)
    {
        return actSuppliesRoleMapper.deleteActSuppliesRoleById(id);
    }

    @Override
    public List<ActSuppliesTemplate> saveSid() {
        return actSuppliesRoleMapper.saveSid();
    }

    @Override
    public int setEnable(Long id, Integer status) {
        updateStatusById(id, status);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("status", status);
        return actSuppliesRoleMapper.setEnable(paramMap);
    }

    @Override
    public int setDisable(Long id, Integer status) {
        updateStatusById(id, status);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("status", status);
        return actSuppliesRoleMapper.setDisable(paramMap);
    }

    @Override
    public List<Integer> findall(Integer[] ids) {
        return actSuppliesRoleMapper.findall(ids);
    }

    @Override
    public List<Integer> find(Integer id) {
        return actSuppliesRoleMapper.find(id);
    }

    /**
     * 发布规则
     *
     * @param ids,tempids 导入规则ID
     * @return 结果
     */
    @Override
    public List<String> releaseRS(Long[] ids , Long[] tempids) {

        List<String> releER = new ArrayList<>();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ActSuppliesRole actSuppliesRole = new ActSuppliesRole();
        ActSuppliesRoleDetail actSuppliesRoleDetail = new ActSuppliesRoleDetail();

        for (int i =0;i<ids.length;i++){

            //查询所属模板是否发布
            Integer integer = actSuppliesRoleMapper.saveTemp(tempids[i]);

            if (integer != null && integer>0){

                actSuppliesRole.setId(ids[i]);
                actSuppliesRole.setStatus(0);
                actSuppliesRole.setUpdateBy(user.getUserName());
                actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleMapper.updateRelease(actSuppliesRole);

                actSuppliesRoleDetail.setRoleId(ids[i].intValue());
                actSuppliesRoleDetail.setStatus(0);
                actSuppliesRoleDetail.setUpdateBy(user.getUserName());
                actSuppliesRoleDetail.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleDetailMapper.updateRoleDetailStatus(actSuppliesRoleDetail);

            }else {
                String name = actSuppliesRoleMapper.saveRoleName(ids[i]);
                releER.add(name);
            }

        }

        return releER;

    }

    /**
     * 下架规则
     *
     * @param ids 导入规则ID
     * @return 结果
     */
    @Override
    public int offShelfRS(Long[] ids) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        ActSuppliesRole actSuppliesRole = new ActSuppliesRole();
        ActSuppliesRoleDetail actSuppliesRoleDetail = new ActSuppliesRoleDetail();

        for (int i=0;i<ids.length;i++){

            //查询历史数据是否引用
            int count = actSuppliesRoleMapper.saveHistory(ids[i]);

            if (count > 0){

                actSuppliesRole.setId(ids[i]);
                actSuppliesRole.setStatus(2);
                actSuppliesRole.setUpdateBy(user.getUserName());
                actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleMapper.updateRelease(actSuppliesRole);

                actSuppliesRoleDetail.setRoleId(ids[i].intValue());
                actSuppliesRoleDetail.setStatus(2);
                actSuppliesRoleDetail.setUpdateBy(user.getUserName());
                actSuppliesRoleDetail.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleDetailMapper.updateRoleDetailStatus(actSuppliesRoleDetail);
            }else {

                actSuppliesRole.setId(ids[i]);
                actSuppliesRole.setStatus(1);
                actSuppliesRole.setUpdateBy(user.getUserName());
                actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleMapper.updateRelease(actSuppliesRole);

                actSuppliesRoleDetail.setRoleId(ids[i].intValue());
                actSuppliesRoleDetail.setStatus(1);
                actSuppliesRoleDetail.setUpdateBy(user.getUserName());
                actSuppliesRoleDetail.setUpdateTime(DateUtils.getNowDate());
                actSuppliesRoleDetailMapper.updateRoleDetailStatus(actSuppliesRoleDetail);
            }

        }

        return 1;

    }


    private void updateStatusById(Long id, Integer status){
        String[] statusText={"禁用","启用"};

        //判断查询结果是否为空
        ActSuppliesRole queryResult = actSuppliesRoleMapper.selectActSuppliesRoleById(Math.toIntExact(id));
        if(queryResult ==null) {
            String message =statusText[status] + "物料总分类管理失败，尝试访问的数据不存在";
            System.out.println(message);
            throw new RuntimeException(message);
        }

        //判断以上查询结果中的status是否与参数status相同
        if(queryResult.getStatus().equals(status)){
            String message = statusText[status] + "物料总分类管理失败，当前物料总分类管理已经处理" +statusText[status] + "状态！";
            System.out.println(message);
            throw new RuntimeException(message);
        }
        ActSuppliesRole actSuppliesRole = new ActSuppliesRole();
        actSuppliesRole.setId(id);
        actSuppliesRole.setStatus(status);

        int rows = actSuppliesRoleMapper.updateActSuppliesRole(actSuppliesRole);
        if (rows != 1) {
            String message = statusText[status] + "物料总分类管理失败，服务器忙请再次重试";
            System.out.println(message);
            throw new RuntimeException(message);
        }
    }
}
