package com.ximai.mes.report.service.impl;

import com.ximai.mes.constant.TaskStatusEnum;
import com.ximai.mes.pro.domain.ProFeedback;
import com.ximai.mes.pro.domain.proWorkOrder.ProWorkorder;
import com.ximai.mes.pro.mapper.ProFeedbackMapper;
import com.ximai.mes.pro.mapper.proWorkOrder.ProWorkorderMapper;
import com.ximai.mes.pro.mapper.task.ProTaskMapper;
import com.ximai.mes.report.request.WorkOrderProgressRequest;
import com.ximai.mes.report.response.WorkOrderProgressFeedbackListResponse;
import com.ximai.mes.report.response.WorkOrderProgressListResponse;
import com.ximai.mes.report.response.WorkOrderProgressProcessListResponse;
import com.ximai.mes.report.service.WorkOrderProgressService;
import com.ximai.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.List;

@Service
public class WorkOrderProgressServiceImp implements WorkOrderProgressService {
    @Autowired
    ProWorkorderMapper proWorkorderMapper;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ProTaskMapper proTaskMapper;
    @Autowired
    private ProFeedbackMapper proFeedbackMapper;
    @Override
    @Transactional
    public List<WorkOrderProgressListResponse> getList(WorkOrderProgressRequest workOrderProgressRequest) {
        List<WorkOrderProgressListResponse>  workOrderProgressListResponses = proWorkorderMapper.getWorkOrderProgressServiceList(workOrderProgressRequest);
        for (WorkOrderProgressListResponse workOrderProgressListResponse : workOrderProgressListResponses){
            workOrderProgressListResponse.setWorkorderType(dictDataService.getDictLabel("mes_workorder_type", workOrderProgressListResponse.getWorkorderType()));
            if(workOrderProgressListResponse.getQuantity() != null && workOrderProgressListResponse.getQuantity().compareTo(BigDecimal.ZERO) >0){
                workOrderProgressListResponse.setQuantityProduced(workOrderProgressListResponse.getQuantityProduced() != null ? workOrderProgressListResponse.getQuantityProduced() : BigDecimal.ZERO);
                workOrderProgressListResponse.setQuantityMu(workOrderProgressListResponse.getQuantityProduced().divide(workOrderProgressListResponse.getQuantity()).setScale(2, RoundingMode.HALF_UP));
            }else{
                workOrderProgressListResponse.setQuantityMu(new BigDecimal(100));

            }
        }
        return workOrderProgressListResponses;
    }

    @Override
    public List<WorkOrderProgressProcessListResponse> getProcessList(String workorderCode) {
        List<WorkOrderProgressProcessListResponse> workOrderProgressProcessListResponses = proTaskMapper.getProcessList(workorderCode);
        for (WorkOrderProgressProcessListResponse workOrderProgressProcessListResponse : workOrderProgressProcessListResponses){
            if(workOrderProgressProcessListResponse.equals(TaskStatusEnum.BEGINNING.getStatus())){
                workOrderProgressProcessListResponse.setStatus("已开工");
            }else if(workOrderProgressProcessListResponse.equals(TaskStatusEnum.FINISHED.getStatus())){
                workOrderProgressProcessListResponse.setStatus("已完工");
            }else{
                workOrderProgressProcessListResponse.setStatus("未开工");
            }
            if(workOrderProgressProcessListResponse.getQuantityProduced() != null && workOrderProgressProcessListResponse.getQuantityProduced().compareTo(BigDecimal.ZERO) > 0){

                workOrderProgressProcessListResponse.setPassRate(workOrderProgressProcessListResponse.getQuantityQualify().
                        divide(workOrderProgressProcessListResponse.getQuantityProduced(),2, RoundingMode.HALF_UP));
            }else{

                workOrderProgressProcessListResponse.setPassRate(BigDecimal.ZERO);
            }
        }
        return workOrderProgressProcessListResponses;
    }

    @Override
    public List<WorkOrderProgressFeedbackListResponse> getFeedbackList(String workorderCode,Long taskId) {
        List<WorkOrderProgressFeedbackListResponse>  workOrderProgressFeedbackListResponses = proFeedbackMapper.getFeedbackList( workorderCode,taskId);
        return workOrderProgressFeedbackListResponses;
    }
}
