package com.xueyi.system.digitalmans.service.impl;

import com.xueyi.common.core.constant.basic.OperateConstants;
import com.xueyi.system.api.digitalmans.domain.vo.DmReceptionVo;
import com.xueyi.system.digitalmans.domain.dto.DmReceptionDto;
import com.xueyi.system.digitalmans.domain.merge.DmReceptionVisitorMerge;
import com.xueyi.system.digitalmans.domain.query.DmReceptionQuery;
import com.xueyi.system.digitalmans.service.IDmReceptionService;
import com.xueyi.system.digitalmans.manager.IDmReceptionManager;
import com.xueyi.common.web.entity.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 接待模式管理管理 服务层处理
 *
 * @author yinruoxi
 */
@Service
public class DmReceptionServiceImpl extends BaseServiceImpl<DmReceptionQuery, DmReceptionDto, IDmReceptionManager> implements IDmReceptionService {

    /**
     * 查询接待模式管理对象列表 | 数据权限
     *
     * @param reception 接待模式管理对象
     * @return 接待模式管理对象集合
     */
    @Override
    //@DataScope(userAlias = "createBy", mapperScope = {"DmReceptionMapper"})
    public List<DmReceptionDto> selectListScope(DmReceptionQuery reception) {
        List<DmReceptionDto> dtoList = baseManager.selectList(reception);
        for (DmReceptionDto dto : dtoList) {
            List<DmReceptionVisitorMerge> rvList = baseManager.selectDeceptionVisitorMerge(dto.getId());
            Long[][] visitorIds = new Long[3][5];
            List<Long> first = new ArrayList<>();
            List<Long> second = new ArrayList<>();
            List<Long> third = new ArrayList<>();
            for (DmReceptionVisitorMerge rv : rvList) {
                switch (rv.getPriority()) {
                    case 1:
                        first.add(rv.getVisitorId());
                        break;
                    case 2:
                        second.add(rv.getVisitorId());
                        break;
                    case 3:
                        third.add(rv.getVisitorId());
                        break;
                    default:
                        break;
                }
            }
            visitorIds[0] = first.toArray(new Long[0]);
            visitorIds[1] = second.toArray(new Long[0]);
            visitorIds[2] = third.toArray(new Long[0]);
            dto.setPriorities(visitorIds);
        }

//        return dtoList;
        return dtoList;
    }


    @Override
    public int updateStatus(DmReceptionDto dto) {
        // 查询原始数据
        DmReceptionDto originDto = baseManager.selectById(dto.getId());
        // 查询运行中的任务
        List <DmReceptionDto> taskList = baseManager.selectReceptionTaskOpenStatus();
        startHandle(OperateConstants.ServiceType.EDIT_STATUS, originDto, dto);
        if (taskList != null && taskList.size() > 0 && dto.getStatus().equals("1")) {
            for (DmReceptionDto task : taskList) {
                if (task.getId().equals(dto.getId())) {
                    continue;
                }
                task.setStatus("0");
                baseManager.updateStatus(task);
            }
        }
        int row = baseManager.updateStatus(dto);
        endHandle(OperateConstants.ServiceType.EDIT_STATUS, row, originDto, dto);
        return row;
    }

    @Override
    public DmReceptionVo selectReceptionTask(Long manId) {
        DmReceptionQuery query = new DmReceptionQuery();
        query.setManId(manId);
        query.setStatus("1");
        List<DmReceptionDto> dtoList = baseManager.selectList(query);
        if (dtoList == null || dtoList.size() == 0) {
            return null;
        }
        DmReceptionVo vo = new DmReceptionVo();
        DmReceptionDto dto = dtoList.get(0);
        vo.setName(dto.getName());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        vo.setDate(dateFormat.format(dto.getReceptionTime()));
        vo.setWelcome(dto.getWelcome());
        vo.setGuide(new Long[]{dto.getHeadId()});
        List<DmReceptionVisitorMerge> rvList = baseManager.selectDeceptionVisitorMerge(dto.getId());
        Long[][] visitorIds = new Long[3][5];
        List<Long> first = new ArrayList<>();
        List<Long> second = new ArrayList<>();
        List<Long> third = new ArrayList<>();
        for (DmReceptionVisitorMerge rv : rvList) {
            switch (rv.getPriority()) {
                case 1:
                    first.add(rv.getVisitorId());
                    break;
                case 2:
                    second.add(rv.getVisitorId());
                    break;
                case 3:
                    third.add(rv.getVisitorId());
                    break;
                default:
                    break;
            }
        }
        vo.setFirst(first.toArray(new Long[0]));
        vo.setSecond(second.toArray(new Long[0]));
        vo.setThird(third.toArray(new Long[0]));
        return vo;
    }
}