package com.inkasso.factoring.system.businessProcessLog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.factoring.system.businessProcessLog.dao.BusinessProcessingLogMapper;
import com.inkasso.factoring.system.businessProcessLog.entity.BusinessProcessingLog;
import com.inkasso.factoring.system.businessProcessLog.service.BusinessProcessingLogService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class BusinessProcessingLogServiceImpl extends ServiceImpl<BusinessProcessingLogMapper, BusinessProcessingLog> implements BusinessProcessingLogService {
//
//    @Autowired
//    private BusinessProcessingLogMapper processingLogMapper;
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private AcceptanceApprovalService acceptanceApprovalService;
//
//    @Autowired
//    private DictionaryService dictionaryService;
//
//    @Override
//    @Transactional
//    public BusinessProcessingLog add(BusinessProcessingLog processingLog) {
//        ValidatorUtil.validator(ValidatorType.ADD, processingLog);
//        User user = ThreadLocalUtil.getUser();
//        if(user != null){
//            processingLog.setUserCreate(ThreadLocalUtil.getUser().getLoginName());
//        }else {
//            processingLog.setUserCreate("admin");
//        }
//        processingLogMapper.add(processingLog);
//        return processingLogMapper.get(processingLog.getId());
//    }
//
//    @Override
//    public BusinessProcessingLog get(Long id) {
//        return processingLogMapper.get(id);
//    }
//
//    @Override
//    @Transactional
//    public Boolean update(BusinessProcessingLog processingLog) {
//        ValidatorUtil.validator(ValidatorType.UPDATE, processingLog);
//        if(StringUtil.isEmpty(processingLog.getUserModified())){
//            processingLog.setUserModified(ThreadLocalUtil.getUser().getLoginName());
//        }
//        return processingLogMapper.update(processingLog) == 1;
//    }
//
//    @Override
//    public List<BusinessProcessingLogDetailVo> findByBusiness(Long businessKey, String businessType) {
//
//        List<String> historicTaskIds;
//        if (businessType.startsWith("FinancialCustomer")) {
//            historicTaskIds = processingLogMapper.findTaskByBusiness(businessKey, businessType);
//        } else {
//            historicTaskIds = processingLogMapper.findTaskByBusinessLike(businessKey, businessType);
//        }
//        if (null != historicTaskIds && historicTaskIds.size() > 0) {
//            List<BusinessProcessingLogDetailVo> businessLogs = new ArrayList<>();
//            for (String historicTaskId : historicTaskIds) {
//                List<BusinessProcessingLog> taskProcessingLogs = processingLogMapper.findLogsByBusiness(businessKey,
//                        businessType, historicTaskId, null, null);
//                if (null != taskProcessingLogs && taskProcessingLogs.size() > 0) {
//                    List<BusinessProcessingLogVo> voList = new ArrayList<>();
//                    String taskName = null;
//                    for (BusinessProcessingLog log : taskProcessingLogs) {
//                        //验收单 通过，不做底层  状态单独处理
//                        taskName = log.getTaskName();
//                        if (businessType.equals("AcceptanceSheet") && taskName.equals("法务审核")) {
//                            AcceptanceApproval acceptanceApproval = acceptanceApprovalService.getById(log.getBusnessKey());
//                            if (acceptanceApproval != null) {
//                                if (acceptanceApproval.getApprovalConclusion().equals(1)) {
//                                    log.setStatus("通过，不做底层");
//                                }
//                            }
//                        }
//                        BusinessProcessingLogVo vo = new BusinessProcessingLogVo();
//                        if (null != log.getAssignee()) {
//                            User assign = userService.get(log.getAssignee().getId());
//                            String assigneeName = assign != null ? assign.getName() : "";
//                            String adminName = "超级管理员";
//                            vo.setAssigneeName(adminName.equals(assigneeName) ? "供应商" : assigneeName);
//                        }
//                        vo.setAssigneeTypeName(ConstantConversionUtil.assigneeType.get(log.getAssigneeType()));
//                        // 上午、下午开始时间换算
//                        String startTimeStr = DateUtil.getGeneralDateFormat(log.getStartTime());
//                        String endTimeStr = DateUtil.getGeneralDateFormat(log.getEndTime());
//                        vo.setStartTime(startTimeStr);
//                        vo.setEndTime(endTimeStr);
////                        if (StringUtil.isNotEmpty(startTime)) {
////                            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(log.getStartTime()))) {
////                                vo.setStartTime(startTime + " " + ConstantUtil.AM_CNAME);
////                            } else {
////                                vo.setStartTime(startTime + " " + ConstantUtil.PM_CNAME);
////                            }
////                        }
////                        if (StringUtil.isNotEmpty(endTime)) {
////                            if (ConstantUtil.AM_CNAME.equals(DateUtil.getCnameOfAMPM(log.getEndTime()))) {
////                                vo.setEndTime(endTime + " " + ConstantUtil.AM_CNAME);
////                            } else {
////                                vo.setEndTime(endTime + " " + ConstantUtil.PM_CNAME);
////                            }
////                        }
//                        if (StringUtil.isNotEmpty(log.getStatus())) {
//                            vo.setStatus(log.getStatus());
//                        }
//                        if (null != log.getOverdueDays() && log.getOverdueDays() > 0) {
//                            vo.setOverdue("逾期" + log.getOverdueDays() + (log.getWorkingDay() ? "个工作日" : "个自然日"));
//                        }
//                        voList.add(vo);
//                    }
//                    businessLogs.add(new BusinessProcessingLogDetailVo(taskName, voList));
//                }
//            }
//
//            return businessLogs;
//        }
//        return null;
//    }
//
//    @Override
//    public BusinessProcessingLog getLatestByTask(Long businessKey, String businessType, String historicTaskId,
//                                                 String assigneeType) {
//        List<BusinessProcessingLog> logs = processingLogMapper.findLogsByBusiness(businessKey, businessType,
//                historicTaskId, assigneeType, true);
//        if (null != logs && logs.size() == 1) {
//            return logs.get(0);
//        }
//        return null;
//    }
//
//    @Override
//    public List<BusinessProcessingLog> findCompletedByBusiness(Long businessKey, String businessType) {
//        return processingLogMapper.findCompletedByBusiness(businessKey, businessType);
//    }
//
//    @Override
//    public List<String> findPendingTaskByBusiness(Long businessKey, String businessType) {
//        return processingLogMapper.findPendingTaskByBusiness(businessKey, businessType);
//    }
//
//    @Override
//    public List<BusinessProcessingLog> findPendingByBusinessKeys(String taskDefKey, String businessType,
//                                                                 List<Long> businessKeys, String assigneeType) {
//        List<String> taskIds = processingLogMapper.findPendingTaskByBusinessKeys(taskDefKey, businessType, businessKeys,
//                assigneeType);
//        List<BusinessProcessingLog> logs = new ArrayList<>();
//        if (null != taskIds && taskIds.size() > 0) {
//            for (String taskId : taskIds) {
//                BusinessProcessingLog log = processingLogMapper.getPendingByTaskId(taskId, assigneeType);
//                /*
//                 * 关键、注意、场景：额度支用审批与评审决议都是【风控经理】，要求一致。如评审决议存在任务分配，如下数据：</br>
//                 *  id|busness_key|business_type|historic_task_id|task_name|form_business_id|assignee_id|assignee_type|end_date|status</br>
//                 *  3078   14      ReviewMeeting 112731           评审决议                     10           processTask   (NULL)            </br>
//                 *  3079   14      ReviewMeeting 112731           评审决议                     41           assignTask    (NULL)
//                 *  3080   14      ReviewMeeting 112731           评审决议     105             30           processTask  2019-06-25 14:02:21 提交  </br>
//                 *  findPendingTaskByBusinessKeys方法返回任务id为112731，然后getPendingByTaskId返回的是id=3080的数据，但是该条数据已经“提交”了。</br>
//                 * 所以，只有符合formBusinessId和endTime都为null才表示真正的待处理，故加如下判断条件。
//                 */
//                if (null != log && null == log.getFormBusinessId() && null == log.getEndTime()) {
//                    logs.add(log);
//                }
//            }
//            return logs;
//        }
//        return logs;// 不建议返回NULL，这里引用易产生空指针异常
//    }
//
//    @Override
//    public List<BusinessProcessingLog> findBusinessProgressStatistics(SearchBusinessProcessingVo search) {
//        return processingLogMapper.findBusinessProgressStatistics(search);
//    }
//
//    @Override
//    public List<BusinessProcessingLog> findBusinessProgressByIds(SearchBusinessProcessingVo search) {
//        return processingLogMapper.findBusinessProgressByIds(search);
//    }
//
//    @Override
//    public BusinessProcessingLog findByHistoricTaskId(String taskId) {
//        return processingLogMapper.findByHistoricTaskId(taskId);
//    }
//
//    @Override
//    public void updateAssigneeId(BusinessProcessingLog processingLog) {
//        processingLogMapper.updateAssigneeId(processingLog);
//    }
//
//    @Override
//    public BusinessProcessingLog getByBusinessTypeBusinessKey(String businessType, Long busnessKey) {
//        return processingLogMapper.getByBusinessTypeBusinessKey(busnessKey, businessType);
//    }
//
//    @Override
//    public List<BusinessProcessingLog> listForNullValueOfEndTime(Long businessKey, String businessType) {
//        return processingLogMapper.listForNullValueOfEndTime(businessKey, businessType);
//    }
//
//    @Override
//    public BusinessProcessingLog findLatestByTaskDefKey(String taskDefKey, String businessType, Long businessKey) {
//        return processingLogMapper.findLatestByTaskDefKey(taskDefKey,businessType,businessKey);
//    }
//
//    @Override
//    public BusinessProcessingLog getbyFormBusinessIdBusinessKeyTaskDefKeyBusinessType(Long formBusinessId, Long businessKey, String taskDefKey, String businessType) {
//        return processingLogMapper.getbyFormBusinessIdBusinessKeyTaskDefKeyBusinessType(formBusinessId,businessKey,taskDefKey,businessType);
//    }
//
//    @Override
//    public BusinessProcessingLog getLastByBusinessKeyBusinessTypeTaskName(Long businessKey, String businessType, String taskName) {
//        return processingLogMapper.getLastByBusinessKeyBusinessTypeTaskName(businessKey,businessType,taskName);
//    }
//
//    @Override
//    public List<BusinessProcessingLog> getCLogByBusinessKeyBusinessType(Long businessKey, String businessType) {
//        return processingLogMapper.getCLogByBusinessKeyBusinessType(businessKey,businessType);
//    }

}
