package com.ruoyi.system.service.impl;

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

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.ActSuppliesRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActSuppliesRoleDetailMapper;
import com.ruoyi.system.domain.ActSuppliesRoleDetail;
import com.ruoyi.system.service.IActSuppliesRoleDetailService;

/**
 * 规则详情Service业务层处理
 *
 * @author dongjg
 * @date 2023-07-13
 */
@Service
public class ActSuppliesRoleDetailServiceImpl implements IActSuppliesRoleDetailService
{
    @Autowired
    private ActSuppliesRoleDetailMapper actSuppliesRoleDetailMapper;

    /**
     * 查询规则详情
     *
     * @param id 规则详情ID
     * @return 规则详情
     */
    @Override
    public ActSuppliesRoleDetail selectActSuppliesRoleDetailById(Integer id)
    {
        return actSuppliesRoleDetailMapper.selectActSuppliesRoleDetailById(id);
    }

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

    /**
     * 新增规则详情
     *
     * @param actSuppliesRoleDetail 规则详情
     * @return 结果
     */
    @Override
    public int insertActSuppliesRoleDetail(ActSuppliesRoleDetail actSuppliesRoleDetail)
    {

        //re:导入表的行，ce:导入表的列，se:导入表的sheet;rt:模板表的行，ct:模板表的列，st:模板表的sheet;ys:运算符

        List<String> filteredList = new ArrayList<>();
        String content2;

        if(actSuppliesRoleDetail.getConvertStatus()==6){
            content2="["+actSuppliesRoleDetail.getCleanIng()+"]";
        }else{
            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()+"\"}") ;

            }
            //  [{"ce": "0", "re": "0","le":"10" , "ze":"1" ,"ct":"1", "rt": "1", "lt": "10", "zt": "2","ys":"1"}]

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

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



//        String content1 = "[{\"se\":\"0\",\"re\":\""+actSuppliesRoleDetail.getDetailYH()+"\",\"ce\":\""+actSuppliesRoleDetail.getDetailYL()+"\",\"st\":\"0\",\"rt\":\""+actSuppliesRoleDetail.getDetailMH()+"\",\"ct\":\""+actSuppliesRoleDetail.getDetailML()+"\",\"ys\":\""+actSuppliesRoleDetail.getConvertStatus()+"\"}]" ;

        actSuppliesRoleDetail.setDetailContent(content2);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesRoleDetail.setCreateBy(user.getUserName());
        actSuppliesRoleDetail.setCreateTime(DateUtils.getNowDate());
        return actSuppliesRoleDetailMapper.insertActSuppliesRoleDetail(actSuppliesRoleDetail);

    }

    /**
     * 修改规则详情
     *
     * @param actSuppliesRoleDetail 规则详情
     * @return 结果
     */
    @Override
    public int updateActSuppliesRoleDetail(ActSuppliesRoleDetail actSuppliesRoleDetail)
    {

        //re:导入表的行，ce:导入表的列，se:导入表的sheet;rt:模板表的行，ct:模板表的列，st:模板表的sheet;ys:运算符
//        String content1 = "[{\"se\":\"0\",\"re\":\""+actSuppliesRoleDetail.getDetailYH()+"\",\"ce\":\""+actSuppliesRoleDetail.getDetailYL()+"\",\"st\":\"0\",\"rt\":\""+actSuppliesRoleDetail.getDetailMH()+"\",\"ct\":\""+actSuppliesRoleDetail.getDetailML()+"\",\"ys\":\""+actSuppliesRoleDetail.getConvertStatus()+"\"}]" ;
        List<String> filteredList = new ArrayList<>();
        String content2;

        if(actSuppliesRoleDetail.getConvertStatus()==6){
            content2="["+actSuppliesRoleDetail.getCleanIng()+"]";
        }else {
            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);

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

        }





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

    }

    /**
     * 批量删除规则详情
     *
     * @param ids 需要删除的规则详情ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesRoleDetailByIds(Integer[] ids)
    {
        return actSuppliesRoleDetailMapper.deleteActSuppliesRoleDetailByIds(ids);
    }

    /**
     * 删除规则详情信息
     *
     * @param id 规则详情ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesRoleDetailById(Integer id)
    {
        return actSuppliesRoleDetailMapper.deleteActSuppliesRoleDetailById(id);
    }

    @Override
    public List<ActSuppliesRole> saveSid() {
        return actSuppliesRoleDetailMapper.saveSid();
    }

//    @Override
//    public List<ActOperation> saveOperation() {
//        return actSuppliesRoleDetailMapper.saveOperation();
//    }


    @Override
    public void setEnable(Long id) {
        updateStatusById(id,0);
    }

    @Override
    public void setDisable(Long id) {
        updateStatusById(id,1);
    }

    @Override
    public List<ActSuppliesRole> saveSidsta() {
        return actSuppliesRoleDetailMapper.saveSidsta();
    }


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

        //判断查询结果是否为空
        ActSuppliesRoleDetail queryResult = actSuppliesRoleDetailMapper.selectActSuppliesRoleDetailById(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);
        }
        ActSuppliesRoleDetail actSuppliesRoleDetail = new ActSuppliesRoleDetail();
        actSuppliesRoleDetail.setId(id);
        actSuppliesRoleDetail.setStatus(status);

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