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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.Constant;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.app.entity.DepartmentsStaffRelateEntity;
import com.lz.modules.flow.dao.FlowDepartmentMapper;
import com.lz.modules.flow.dao.FlowManagerMapper;
import com.lz.modules.flow.entity.FlowDepartment;
import com.lz.modules.flow.entity.FlowManager;
import com.lz.modules.flow.model.TypeFlowDto;
import com.lz.modules.flow.service.FlowDepartmentService;
import com.lz.modules.sys.service.app.ResultRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

@Service
public class FlowDepartmentServiceImpl extends ServiceImpl<FlowDepartmentMapper, FlowDepartment> implements FlowDepartmentService {


    @Autowired
    private FlowDepartmentMapper flowDepartmentMapper;

    @Autowired
    private FlowDepartmentService flowDepartmentService;

    @Autowired
	private ResultRecordService resultRecordService;

    @Autowired
	private FlowManagerMapper flowManagerMapper;


    @Override
    public FlowDepartment selectFlowDepartmentById(Long id) {
        return flowDepartmentMapper.selectFlowDepartmentById(id);
    }


    @Override
    public Long insertFlowDepartment(FlowDepartment flowDepartment) {
        return flowDepartmentMapper.insertFlowDepartment(flowDepartment);
    }


    @Override
    public int updateFlowDepartmentById(FlowDepartment flowDepartment) {
        return flowDepartmentMapper.updateFlowDepartmentById(flowDepartment);
    }


    @Override
    public int updateCoverFlowDepartmentById(FlowDepartment flowDepartment) {
        return flowDepartmentMapper.updateCoverFlowDepartmentById(flowDepartment);
    }


    @Override
    public int deleteFlowDepartmentById(Long id) {
        return flowDepartmentMapper.deleteFlowDepartmentById(id);
    }

    @Override
    public FlowDepartment selectByStaffId(Long staffId) {
        return flowDepartmentMapper.selectByStaffId(staffId);
    }

    @Override
    public FlowDepartment selectByParentId(Long parentId) {
        return flowDepartmentMapper.selectByParentId(parentId);
    }

    @Override
    public R selectFlowManager(Long staffId) {
        Long flowId1 = 0l;
        Long flowId2 = 0l;
        FlowDepartment flowDepartment = flowDepartmentService.selectByStaffId(staffId);
        if (flowDepartment == null) {
			DepartmentsStaffRelateEntity leader = resultRecordService.getLeaderDepartmentsStaffRelateEntity(staffId);
			if(leader != null){
                flowDepartment = flowDepartmentService.selectByStaffId(leader.getStaffId());
                flowId1 = TypeFlowDto.getFlowId(flowDepartment.getFlowIds(), Constant.CHILD, 1);       // 表示是子
                flowId2 = TypeFlowDto.getFlowId(flowDepartment.getFlowIds(), Constant.CHILD, 2);       // 表示是子
            }
        } else {
            flowId1 = TypeFlowDto.getFlowId(flowDepartment.getFlowIds(), Constant.SELF, 1);       // 表示是部门主管自己
            flowId2 = TypeFlowDto.getFlowId(flowDepartment.getFlowIds(), Constant.SELF, 2);       // 表示是部门主管自己
        }
		List<FlowManager> flowManagers = new ArrayList<>();
        flowManagers.add(getFlowMnagers(flowId1,"type1"));
        flowManagers.add(getFlowMnagers(flowId2,"type2"));

		PageUtils pageUtils = new PageUtils();
        pageUtils.setList(flowManagers);
        return R.ok().put("page", pageUtils);
    }

    public FlowManager getFlowMnagers(Long flowId, String type) {
        FlowManager flowManager = flowManagerMapper.selectFlowManagerById(flowId);
        if (flowManager == null) {
            flowManager = new FlowManager();
            flowManager.setId(flowId);
            flowManager.setName(flowId == 0 ? "无" : flowId + "");
        }
        flowManager.setType(type);
        return flowManager;
    }


}
