package org.jeecg.modules.orderplatform.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.orderplatform.mapper.PlatformQueryOtherMapper;
import org.jeecg.modules.orderplatform.service.IPlatformQueryOtherService;
import org.jeecg.modules.orderplatform.util.JsonArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PlatformQueryOtherServiceImpl implements IPlatformQueryOtherService {

    @Autowired
    PlatformQueryOtherMapper platformQueryOtherMapper;

    @Override
    public List<JSONObject> getAllDesignLeader() {
        return platformQueryOtherMapper.queryAllDesignLeader();
    }

    @Override
    public Map<String, Object> operatorJobContent() {
        Map<String, Object> result=new HashMap<>();
        String currentLogon=getCurrentLogon();
        //我发起的订单
        result.put("newOrder",platformQueryOtherMapper.queryNewOrderCountByOperator(currentLogon));
        //待审核的物料
        result.put("toAuditMaterial",platformQueryOtherMapper.queryMaterialCountByOperatorUserId(currentLogon));
        //已完成的订单
        result.put("finishOrder",platformQueryOtherMapper.queryFinishOrderCountByOperator(currentLogon));
        //被驳回的
        result.put("rejectOrder",platformQueryOtherMapper.queryRejectOrderCountByOperator(currentLogon));
        //全部订单
        result.put("allOrder",platformQueryOtherMapper.queryNewOrderCountByOperator(currentLogon));

        return result;

    }

    @Override
    public Map<String, Object> designJobContent() {
        Map<String, Object> result=new HashMap<>();
        String currentLogon=getCurrentLogon();
        //指派给我的物料（待完成）
        result.put("myMaterial", getMaterialCountByDesignUserId(currentLogon,"1346"));
        //被驳回的物料
        result.put("rejectMaterial", getMaterialCountByDesignUserId(currentLogon,"36"));
        //已完成的物料
        result.put("finishMaterial", getMaterialCountByDesignUserId(currentLogon,"7"));
        //全部物料
        result.put("allMaterial", getMaterialCountByDesignUserId(currentLogon,"1234567"));
        return result;
    }

    @Override
    public Map<String, Object> designLeaderJobContent() {
        Map<String, Object> result=new HashMap<>();
        String currentLogon=getCurrentLogon();
        //待确定的订单
        result.put("toDecided", platformQueryOtherMapper.queryOrderCountByDesign(currentLogon));
        //带指派的物料
        result.put("toPayOut", platformQueryOtherMapper.queryMaterialCountByDesign(currentLogon));
        //指派给我的物料（待完成）
        result.put("myMaterial", getMaterialCountByDesignUserId(currentLogon,"1234567"));
        //被驳回的物料
        result.put("rejectMaterial", getMaterialCountByDesignUserId(currentLogon,"36"));
        //已完成订单
        result.put("finishOrder", platformQueryOtherMapper.queryFinishOrderCountByDesign(currentLogon));
        //全部订单
        result.put("allOrder",platformQueryOtherMapper.queryAllOrderByDesign(currentLogon));
        return result;
    }

    @Override
    public JSONObject queryIdentity() {
        return platformQueryOtherMapper.queryIdentity(getCurrentLogon());
    }

    private String getCurrentLogon(){
        return ((LoginUser) SecurityUtils.getSubject().getPrincipal()).getId();
    }

    private int getMaterialCountByDesignUserId(String userId,String materialStatus){
        JSONArray materialStatusJsonArray = JsonArrayUtil.Varchar2JSONArray(materialStatus);
        return platformQueryOtherMapper.queryMaterialCountByDesignUserId(userId,materialStatusJsonArray);
    }
}
