package com.ruoyi.system.service.impl;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.ActSuppliesTemplate;
import com.ruoyi.system.domain.MyListItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ActSuppliesConvertroleMapper;
import com.ruoyi.system.domain.ActSuppliesConvertrole;
import com.ruoyi.system.service.IActSuppliesConvertroleService;

/**
 * 通用规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-29
 */
@Service
public class ActSuppliesConvertroleServiceImpl implements IActSuppliesConvertroleService
{
    @Autowired
    private ActSuppliesConvertroleMapper actSuppliesConvertMapper;

    /**
     * 查询通用规则
     * 
     * @param id 通用规则ID
     * @return 通用规则
     */
    @Override
    public ActSuppliesConvertrole selectActSuppliesConvertById(Long id)
    {
        ActSuppliesConvertrole actSuppliesConvertrole = actSuppliesConvertMapper.selectActSuppliesConvertById(id);

        String content = actSuppliesConvertrole.getContent();

        ObjectMapper objectMapper = new ObjectMapper();
        MyListItem[] items = new MyListItem[0];
        try {
            items = objectMapper.readValue(content, MyListItem[].class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        List<MyListItem> list = new ArrayList<>();
        for (int i=0;i<items.length;i++){

            list.add(items[i]);

        }
        actSuppliesConvertrole.setList(list);
        return actSuppliesConvertrole;
    }

    /**
     * 查询通用规则列表
     * 
     * @param actSuppliesConvertrole 通用规则
     * @return 通用规则
     */
    @Override
    public List<ActSuppliesConvertrole> selectActSuppliesConvertList(ActSuppliesConvertrole actSuppliesConvertrole)
    {
        return actSuppliesConvertMapper.selectActSuppliesConvertList(actSuppliesConvertrole);
    }

    /**
     * 新增通用规则
     * 
     * @param actSuppliesConvertrole 通用规则
     * @return 结果
     */
    @Override
    public int insertActSuppliesConvert(ActSuppliesConvertrole actSuppliesConvertrole)
    {

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


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

            filteredList.add("{\"ce\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailCE()[i])-1)+"\",\"re\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailRE()[i])-1)+"\",\"le\":\""+(actSuppliesConvertrole.getDetailLE()[i])+"\",\"ze\":\""+(actSuppliesConvertrole.getDetailZE()[i])+"\",\"ct\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailCT()[i])-1)+"\",\"rt\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailRT()[i])-1)+"\",\"lt\":\""+(actSuppliesConvertrole.getDetailLT()[i])+"\",\"zt\":\""+(actSuppliesConvertrole.getDetailZT()[i])+"\",\"ys\":\""+(actSuppliesConvertrole.getDetailYS()[i])+"\"}") ;

        }


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

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

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

        actSuppliesConvertrole.setContent(content2);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesConvertrole.setCreateBy(user.getUserName());
        actSuppliesConvertrole.setCreateTime(DateUtils.getNowDate());
        return actSuppliesConvertMapper.insertActSuppliesConvert(actSuppliesConvertrole);
    }

    /**
     * 修改通用规则
     * 
     * @param actSuppliesConvertrole 通用规则
     * @return 结果
     */
    @Override
    public int updateActSuppliesConvert(ActSuppliesConvertrole actSuppliesConvertrole)
    {

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


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

            filteredList.add("{\"ce\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailCE()[i])-1)+"\",\"re\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailRE()[i])-1)+"\",\"le\":\""+(actSuppliesConvertrole.getDetailLE()[i])+"\",\"ze\":\""+(actSuppliesConvertrole.getDetailZE()[i])+"\",\"ct\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailCT()[i])-1)+"\",\"rt\":\""+(Integer.parseInt(actSuppliesConvertrole.getDetailRT()[i])-1)+"\",\"lt\":\""+(actSuppliesConvertrole.getDetailLT()[i])+"\",\"zt\":\""+(actSuppliesConvertrole.getDetailZT()[i])+"\",\"ys\":\""+(actSuppliesConvertrole.getDetailYS()[i])+"\"}") ;

        }


        //  [{"ce": "0", "re": "0", "le": "6","ze": "1", "ct": "0", "rt": "0","lt":"6","zt":"1","ys": "0"}]
        String content = String.join(",",filteredList);
        String content2 = "["+content+"]";

        actSuppliesConvertrole.setContent(content2);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesConvertrole.setUpdateBy(user.getUserName());
        actSuppliesConvertrole.setUpdateTime(DateUtils.getNowDate());
        return actSuppliesConvertMapper.updateActSuppliesConvert(actSuppliesConvertrole);
    }

    /**
     * 批量删除通用规则
     * 
     * @param ids 需要删除的通用规则ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesConvertByIds(Long[] ids)
    {
        return actSuppliesConvertMapper.deleteActSuppliesConvertByIds(ids);
    }

    /**
     * 删除通用规则信息
     * 
     * @param id 通用规则ID
     * @return 结果
     */
    @Override
    public int deleteActSuppliesConvertById(Long id)
    {
        return actSuppliesConvertMapper.deleteActSuppliesConvertById(id);
    }

    @Override
    public List<ActSuppliesTemplate> selectActSuppliesConvertListTemp() {
        return actSuppliesConvertMapper.selectActSuppliesConvertListTemp();
    }

    @Override
    public List<ActSuppliesConvertrole> convertRoleList(Integer id) {
        return actSuppliesConvertMapper.convertRoleList(id);
    }

    @Override
    public List<ActSuppliesConvertrole> convertRoleListid(Integer id) {
        return actSuppliesConvertMapper.convertRoleListid(id);
    }
}
