package com.lz.modules.flow.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.Constant;
import com.lz.common.utils.PageUtils;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.app.utils.t.OneTuple;
import com.lz.modules.equipment.entity.model.BasePage;
import com.lz.modules.flow.dao.FlowRecordMapper;
import com.lz.modules.flow.entity.FlowRecord;
import com.lz.modules.flow.enums.FlowRecordEnum;
import com.lz.modules.flow.model.StaffRoleDto;
import com.lz.modules.flow.service.FlowRecordService;
import com.lz.modules.performance.req.AssessTaskReq;
import com.lz.modules.sys.entity.app.ResultRecord;
import com.lz.modules.sys.service.app.ResultRecordService;
import lombok.Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* <p>
* 流转记录表 服务类
* </p>
*
* @author quyixiao
* @since 2020-08-18
*/

@Service
public class FlowRecordServiceImpl extends ServiceImpl<FlowRecordMapper, FlowRecord> implements FlowRecordService {


    @Autowired
	private FlowRecordMapper flowRecordMapper;

    @Autowired
	private StaffService staffService;

    @Autowired
	private ResultRecordService resultRecordService;



	@Override
	public FlowRecord selectFlowRecordById(Long id){
		return flowRecordMapper.selectFlowRecordById(id);
	}



	@Override
	public Long insertFlowRecord(FlowRecord flowRecord){
		return flowRecordMapper.insertFlowRecord(flowRecord);
	}



	@Override
	public int updateFlowRecordById(FlowRecord flowRecord){
		return flowRecordMapper.updateFlowRecordById(flowRecord);
	}



	@Override
	public int updateCoverFlowRecordById(FlowRecord flowRecord){
		return flowRecordMapper.updateCoverFlowRecordById(flowRecord);
	}

	@Override
	public int deleteFlowRecordById(Long id){
		return flowRecordMapper.deleteFlowRecordById(id);
	}

    @Override
    public FlowRecord selectLastFlowRecordByRecordId(Long recordId) {
        return flowRecordMapper.selectLastFlowRecordByRecordId(recordId);
    }

    @Override
    public FlowRecord selectNotApprovalStaffIdFlowRecords(Long recordId) {
        return flowRecordMapper.selectNotApprovalStaffIdFlowRecords(recordId);
    }

    @Override
    public List<FlowRecord> selectFlowRecordByRecordId(Long recordId) {
        return flowRecordMapper.selectFlowRecordByRecordId(recordId);
    }


	@Override
	public void initFlowRecord(ResultRecord resultRecord, Long roleId, Integer type, String name) {
		StaffEntity staffEntity = staffService.selectStaffById(resultRecord.getStaffId());
		FlowRecord flowRecord = new FlowRecord();
		flowRecord.setFlowStaffIdRole(StaffRoleDto.getStaffRole(resultRecord.getStaffId(), roleId));
		OneTuple<Long> flowInfo = resultRecordService.getFlowInfo(resultRecord.getStaffId(), type);
		flowRecord.setFlowId(flowInfo.getFirst());
		flowRecord.setApprovalStaffId(resultRecord.getStaffId());
		flowRecord.setApprovalStaffName(staffEntity.getName());
		flowRecord.setFlowIndex(0);
		flowRecord.setFlowName(staffEntity.getName() + "-" + name);
		flowRecord.setRecordStaffId(resultRecord.getStaffId());
		flowRecord.setRecordId(resultRecord.getId());
		flowRecord.setStatus(0);
		flowRecord.setType(resultRecord.getType());
		flowRecord.setDepartmentLevel(Constant.ME);
		flowRecord.setStatusName(FlowRecordEnum.START.getName());
		flowRecordMapper.insertFlowRecord(flowRecord);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByResultRecordIdFlowId(Long recordResultId) {
		return flowRecordMapper.selectFlowRecordByResultRecordIdFlowId(recordResultId);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByFlowId(Long recordId) {
		return flowRecordMapper.selectFlowRecordByFlowId(recordId);
	}

	@Override
	public FlowRecord selectLastFlowNoStatusRecordByRecordId(Long resultRecordId) {
		return flowRecordMapper.selectLastFlowNoStatusRecordByRecordId(resultRecordId);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByResultRecordIdType(Long resultRecordId, Integer type) {
		return flowRecordMapper.selectFlowRecordByResultRecordIdType(resultRecordId,type);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByResultRecordIdTypeStatus(Long recordResultId, Integer type, int status) {
		return flowRecordMapper.selectFlowRecordByResultRecordIdTypeStatus(recordResultId,type,status);
	}

	@Override
	public Long copyFlowRecord(FlowRecord flowRecord) {
		return flowRecordMapper.copyFlowRecord(flowRecord);
	}

	@Override
	public PageUtils selectTaskListByApprovalStaffId(AssessTaskReq req, Long approvalStaffId) {
		PageUtils pageUtils = new PageUtils();
		if(req.getStatus()==0){
			pageUtils = PageUtils.startPage(req.getCurrPage(), req.getPageSize()).doSelect(
					page -> flowRecordMapper.selectTodoTasksByApprovalStaffId(approvalStaffId,page)
			);
		}
		if(req.getStatus()==1){
			pageUtils = PageUtils.startPage(req.getCurrPage(), req.getPageSize()).doSelect(
					page -> flowRecordMapper.selectFinishTasksByApprovalStaffId(approvalStaffId,page)
			);
		}

		return pageUtils;
	}

    @Override
    public void insertFlowRecords(List<FlowRecord> flowRecords) {
		flowRecordMapper.insertFlowRecords(flowRecords);
    }

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdStatus(Long resultRecordId,Integer status) {
		return flowRecordMapper.selectFlowRecordByRecordIdStatus(resultRecordId,status);
	}

	@Override
	public FlowRecord selectFlowRecordByRecordIdAndFlowId(Long resultRecordId, Long flowId) {
		return flowRecordMapper.selectFlowRecordByRecordIdAndFlowId(resultRecordId,flowId);
	}

    @Override
    public List<FlowRecord> selectFlowRecordByRecordIdStatusFlowIndex(Long resultRecordId, int status, int flowIndex) {
        return flowRecordMapper.selectFlowRecordByRecordIdStatusFlowIndex(resultRecordId,status,flowIndex);
    }

	@Override
	public FlowRecord selectPreFlowRecordByRecordIdMaxIdStatus(Long resultRecordId, Long id, int status) {
		return flowRecordMapper.selectPreFlowRecordByRecordIdMaxIdStatus(resultRecordId,id,status);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdFlowIndex(Long resultRecordId, Integer flowIndex) {
		return flowRecordMapper.selectFlowRecordByRecordIdFlowIndex(resultRecordId,flowIndex);
	}

    @Override
    public List<FlowRecord> selectFlowRecordByResultRecordId(Long resultRecordId) {

		return flowRecordMapper.selectFlowRecordByResultRecordId(resultRecordId);
    }

	@Override
	public FlowRecord selectFlowRecordByRecordIdMinIdStatus(Long resultRecordId, Long id, int status) {
		return flowRecordMapper.selectFlowRecordByRecordIdMinIdStatus(resultRecordId,id,status);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdFlowProcess(Long id, int flowProcess){
		return flowRecordMapper.selectFlowRecordByRecordIdFlowProcess(id,flowProcess);
	}

	@Override
	public List<FlowRecord> selectAndOrFlowRecordsById(Long flowRecordId, Long recordId){
		return flowRecordMapper.selectAndOrFlowRecordsById(flowRecordId, recordId);
	}

	@Override
	public List<FlowRecord> selectLastFlowRecordsById(Long recordId){
		return flowRecordMapper.selectLastFlowRecordsById(recordId);
	}

	@Override
	public List<FlowRecord> selectLastFlowRecordsByIdAndFlowIndex(Long recordId, int flowIndex, int status){
		return flowRecordMapper.selectLastFlowRecordsByIdAndFlowIndex(recordId, flowIndex, status);
	}

	@Override
	public List<FlowRecord> selectSkipFlowRecordsById(Long recordId){
		return flowRecordMapper.selectSkipFlowRecordsById(recordId);
	}

	@Override
	public List<FlowRecord> selectFirstFlowRecordsByIdAndFlowIndex(Long recordId, int flowIndex, int status){
		return flowRecordMapper.selectFirstFlowRecordsByIdAndFlowIndex(recordId, flowIndex, status);
	}

	@Override
	public List<FlowRecord> selectFlowRecordsByRecordIdAndStatus(Long recordId, int status){
		return flowRecordMapper.selectFlowRecordsByRecordIdAndStatus(recordId, status);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdGeFlowIndex(Long recordId, Integer flowIndex) {
		return flowRecordMapper.selectFlowRecordByRecordIdGeFlowIndex(recordId,flowIndex);
	}

	@Override
	public void deleteFlowRecordByRecordId(Long recordId) {
		flowRecordMapper.deleteFlowRecordByRecordId(recordId);
	}

	@Override
	public void updateFlowRecordIdById(Long id, Long newId) {
		flowRecordMapper.updateFlowRecordIdById(id,newId);
	}

	@Override
	public FlowRecord selectPreFlowRecordByRecordIdMinIdStatusList(Long recordId, Integer flowIndex, List<Integer> statusList) {
		return flowRecordMapper.selectPreFlowRecordByRecordIdMinIdStatusList(recordId,flowIndex,statusList);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdFlowIndexStatusList(Long recordId, Integer flowIndex, List<Integer> statusList) {
		return flowRecordMapper.selectFlowRecordByRecordIdFlowIndexStatusList(recordId,flowIndex,statusList);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdLeFlowIndex(Long resultRecordId, Integer flowIndex) {
		return flowRecordMapper.selectFlowRecordByRecordIdLeFlowIndex(resultRecordId,flowIndex);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdsFlowProcess(List<Long> recordIds, Long flowProcess){
		return flowRecordMapper.selectFlowRecordByRecordIdsFlowProcess(recordIds,flowProcess);
	}

	@Override
	public List<FlowRecord> selectFlowRecordByRecordIdsStatus(List<Long> recordIds, int status){
		return flowRecordMapper.selectFlowRecordByRecordIdsStatus(recordIds,status);
	}

}
