package com.tofly.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.DepartmentClient;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.pojo.Department;
import com.tofly.qualitycontrol.common.QualityControlConstant;
import com.tofly.qualitycontrol.entity.RejectedRecord;
import com.tofly.qualitycontrol.mapper.RejectedRecordMapper;
import com.tofly.qualitycontrol.service.IRejectedRecordService;
import com.tofly.qualitycontrol.vo.RejectedRecordVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

import static com.google.common.collect.Lists.newArrayList;
import static com.tofly.qualitycontrol.util.QualityControlUtil.*;
import static java.lang.Long.valueOf;

/**
 * @author B
 */
@Service
@RequiredArgsConstructor
public class RejectedRecordServiceImpl extends ServiceImpl<RejectedRecordMapper,RejectedRecord> implements IRejectedRecordService, QualityControlConstant {

	private final DepartmentClient departmentClient;

	@Override
	public List<RejectedRecordVO> getList(String certificationNumber) {
		List<RejectedRecordVO> voList = newArrayList();

		LambdaQueryWrapper<RejectedRecord> queryWrapper = Wrappers.<RejectedRecord>lambdaQuery().eq(StringUtils.isNotEmpty(certificationNumber),RejectedRecord::getCertificationNumber,certificationNumber).orderByDesc(RejectedRecord::getCreateTime);
		Map<Long,String> deptMap = new HashMap<>();
		this.list(queryWrapper).forEach(l -> {
			RejectedRecordVO vo = getVO(l,RejectedRecordVO.class);
			vo.setExecuteDeptName(deptName(deptMap,vo.getExecuteDept()));
			vo.setCurrentProcessName(getProcessName(vo.getCurrentProcess()));
			String executeDept = vo.getExecuteDept();
			Boolean flag1 = isNotNull(executeDept);
			if (flag1) {
				vo.setExecuteDept(departmentClient.getById(valueOf(executeDept)).getResult().getName());
			}
			voList.add(vo);
		});
		return voList;
	}

	@Override
	public IPage<RejectedRecordVO> getList(String certificationNumber,Page<RejectedRecord> page) {
		Boolean flag = isNotNull(certificationNumber);

		List<RejectedRecordVO> voList = newArrayList();

		LambdaQueryWrapper<RejectedRecord> queryWrapper = Wrappers.<RejectedRecord>lambdaQuery().eq(flag,RejectedRecord::getCertificationNumber,certificationNumber).orderByDesc(RejectedRecord::getCreateTime);
		Map<Long,String> deptMap = new HashMap<>();
		page(page,queryWrapper).getRecords().forEach(l -> {
			RejectedRecordVO vo = getVO(l,RejectedRecordVO.class);
			vo.setExecuteDeptName(deptName(deptMap,vo.getExecuteDept()));
			vo.setCurrentProcessName(getProcessName(vo.getCurrentProcess()));
			String executeDept = vo.getExecuteDept();
			Boolean flag1 = isNotNull(executeDept);
			if (flag1) {
				vo.setExecuteDept(departmentClient.getById(valueOf(executeDept)).getResult().getName());
			}

			voList.add(vo);
		});

		return getIPage(page,voList,count(queryWrapper));
	}
	public String getProcessName(String currentProcess) {
		return StringUtils.isEmpty(currentProcess) || "0".equals(currentProcess) ? "检验记录" : "1".equals(currentProcess) ? "整改下达" : "2".equals(currentProcess) ? "整改处置" : "3".equals(currentProcess) ? "整改审核" : "4".equals(currentProcess) ? "整改验证" : "5".equals(currentProcess) ? "验证审核" : "其它";
	}
	private String deptName(Map<Long,String> deptMap, String deptkey){
		if(StringUtils.isNotEmpty(deptkey)) {
			Long key = Long.parseLong(deptkey);
			if (deptMap.containsKey(key)){
				return deptMap.get(key);
			}else {
				ResultRespone<Department> rr = departmentClient.getById(key);
				if(rr==null||rr.getResult()==null){
					deptMap.put(key,"");
				}
				String deptName = rr.getResult().getName();
				deptMap.put(key,deptName);
				return deptName;
			}
		}
		return null;
	}

}
