package com.ruoyi.system.service.impl;

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 com.ruoyi.system.domain.ActSuppliesTemplate;
import com.ruoyi.system.mapper.ActSuppliesRoleMapper;
import com.ruoyi.system.mapper.ActSuppliesTemplateMapper;
import com.ruoyi.system.service.ActSuppliesTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 物资管理Service业务层处理
 * 
 * @author
 * @date
 */
@Service
public class ActSuppliesTemplateServiceImpl implements ActSuppliesTemplateService
{
    @Autowired
    private ActSuppliesTemplateMapper actSuppliesTemplateMapper;

    @Autowired
    private  ActSuppliesRoleMapper actSuppliesRoleMapper;


    @Override
    public List<ActSuppliesTemplate> selectSuppliesTemplateList(ActSuppliesTemplate sysSupplies) {
        return actSuppliesTemplateMapper.selectSuppliesTemplateList(sysSupplies);
    }

    @Override
    public List<ActSuppliesTemplate> selectSuppliesTemplateById(Long id) {
        return actSuppliesTemplateMapper.selectSuppliesTemplateById(id);
    }

    @Override
    public int insertSuppliesTemplate(ActSuppliesTemplate sysSupplies) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sysSupplies.setCreateBy(user.getUserName());
        sysSupplies.setCreateTime(DateUtils.getNowDate());
        return actSuppliesTemplateMapper.insertSuppliesTemplate(sysSupplies);
    }
//
//    @Override
//    public List<ActSuppliesTemplate> selectlistnameid() {
//        return actSuppliesTemplateMapper.selectlistnameid();
//    }

    @Override
    public int updateSuppliesTemplate(ActSuppliesTemplate sysSupplies) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sysSupplies.setUpdateBy(user.getUserName());
        sysSupplies.setUpdateTime(DateUtils.getNowDate());
        return actSuppliesTemplateMapper.updateSuppliesTemplate(sysSupplies);
    }
    /**
     * 查询导入规则
     *
     * @param id 导入规则
     * @return 导入规则
     */
    @Override
    public ActSuppliesRole selectActSuppliesRoleById(Integer id) {
        return actSuppliesTemplateMapper.selectActSuppliesRoleById(id);
    }

    @Override
    public List<ActSuppliesRole> selectActSuppliesRoleList(Integer id) {
        return actSuppliesTemplateMapper.selectActSuppliesRoleList(id);
    }

    @Override
    public int updateActSuppliesRole(ActSuppliesRole actSuppliesRole) {

        actSuppliesRole.setUpdateTime(DateUtils.getNowDate());
        return actSuppliesTemplateMapper.updateActSuppliesRole(actSuppliesRole);
    }

    @Override
    public List<String> deleteSuppliesTemplateByIds(Long[] ids) {

        List<ActSuppliesTemplate> actSuppliesTemplate = actSuppliesTemplateMapper.saveTemplateStatus(ids);
        List<String> releER = new ArrayList<>();

        for (int i=0;i<actSuppliesTemplate.size();i++) {

            SysUser user = SecurityUtils.getLoginUser().getUser();
            if (actSuppliesTemplate.get(i).getStatus()==0) {
                int count = actSuppliesTemplateMapper.selectHistory(actSuppliesTemplate.get(i).getId());

                if(count>0){

                    actSuppliesTemplate.get(i).setStatus(2);
                    actSuppliesTemplate.get(i).setUpdateBy(user.getUserName());
                    actSuppliesTemplate.get(i).setUpdateTime(DateUtils.getNowDate());
                    actSuppliesTemplateMapper.updateRelease(actSuppliesTemplate.get(i));

                }else {

                    actSuppliesTemplate.get(i).setStatus(1);
                    actSuppliesTemplate.get(i).setUpdateBy(user.getUserName());
                    actSuppliesTemplate.get(i).setUpdateTime(DateUtils.getNowDate());
                    actSuppliesTemplateMapper.updateRelease(actSuppliesTemplate.get(i));

                }

            } else if (actSuppliesTemplate.get(i).getStatus()==1) {

                Long findall = actSuppliesTemplateMapper.findall(actSuppliesTemplate.get(i).getId());
                if (findall>0){
                    releER.add(actSuppliesTemplate.get(i).getTemplateName());
                }
                actSuppliesTemplateMapper.deleteSuppliesTemplateById(actSuppliesTemplate.get(i).getId());

            }
        }

        return releER;

    }

    @Override
    public List<ActSuppliesTemplate> saveTemplate(Long id) {
        return actSuppliesTemplateMapper.saveTemplate(id);
    }


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

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

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

    @Override
    public List<String> findName() {
        return actSuppliesTemplateMapper.findName();
    }


    @Override
    public int releaseStatus(Long[] ids) {

        List<ActSuppliesTemplate> actSuppliesTemplate = actSuppliesTemplateMapper.saveTemplateStatus(ids);

        for (int i =0;i<actSuppliesTemplate.size();i++) {

            SysUser user = SecurityUtils.getLoginUser().getUser();
            actSuppliesTemplate.get(i).setUpdateBy(user.getUserName());
            actSuppliesTemplate.get(i).setUpdateTime(DateUtils.getNowDate());
            if (actSuppliesTemplate.get(i).getStatus() > 0){
                actSuppliesTemplate.get(i).setStatus(0);
                actSuppliesTemplateMapper.updateSuppliesTemplate(actSuppliesTemplate.get(i));
            }

        }
        return 1;
    }

    @Override
    public int offShelfStatus(Long[] ids) {

        List<ActSuppliesTemplate> actSuppliesTemplate = actSuppliesTemplateMapper.saveTemplateStatus(ids);

        for (int i =0;i<actSuppliesTemplate.size();i++){

            SysUser user = SecurityUtils.getLoginUser().getUser();
            actSuppliesTemplate.get(i).setUpdateBy(user.getUserName());
            actSuppliesTemplate.get(i).setUpdateTime(DateUtils.getNowDate());

            if (actSuppliesTemplate.get(i).getStatus() ==0){
                int count = actSuppliesTemplateMapper.selectHistory(actSuppliesTemplate.get(i).getId());
                if (count == 0){
                    actSuppliesTemplate.get(i).setStatus(1);
                    actSuppliesTemplateMapper.updateSuppliesTemplate(actSuppliesTemplate.get(i));
                }else {
                    actSuppliesTemplate.get(i).setStatus(2);
                    actSuppliesTemplateMapper.updateSuppliesTemplate(actSuppliesTemplate.get(i));
                }
            }

        }
        return 1;

    }

    @Override
    public String copyTemplate(Long id) {


        List<ActSuppliesTemplate> actSuppliesTemplates = actSuppliesTemplateMapper.selectSuppliesTemplateByIdNoId(id);

        actSuppliesTemplates.get(0).setStatus(1);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesTemplates.get(0).setCreateBy(user.getCreateBy());
        actSuppliesTemplates.get(0).setCreateTime(DateUtils.getNowDate());

        actSuppliesTemplateMapper.insertSuppliesTemplate(actSuppliesTemplates.get(0));

        return actSuppliesTemplates.get(0).getTemplateName();

    }




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

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

        //判断以上查询结果中的status是否与参数status相同

        ActSuppliesTemplate actSuppliesTemplate = new ActSuppliesTemplate();
        actSuppliesTemplate.setId(id);
        actSuppliesTemplate.setStatus(status);

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

}
