package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.HyOpenAreaInfoDAO;
import com.cool.store.dao.LineAuditInfoDAO;
import com.cool.store.dao.LineInfoDAO;
import com.cool.store.entity.*;
import com.cool.store.enums.AuditResultTypeEnum;
import com.cool.store.mapper.AuditStatusMapper;
import com.cool.store.mapper.LineInterviewMapper;
import com.cool.store.request.AuditCheckRequest;
import com.cool.store.request.AuditDetailRequest;
import com.cool.store.response.AuditDetailResponse;
import com.cool.store.response.CheckListResponse;
import com.cool.store.service.AuditStatusService;
import com.cool.store.utils.poi.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AuditStatusServiceImpl implements AuditStatusService {

    @Resource
    AuditStatusMapper auditStatusMapper;

    @Resource
    private LineInfoDAO lineInfoDAO;

    @Resource
    private LineAuditInfoDAO lineAuditInfoDAO;

    @Resource
    private LineInterviewMapper lineInterviewMapper;

    @Resource
    HyOpenAreaInfoDAO hyOpenAreaInfoDAO;

    @Resource
    EnterpriseUserDAO enterpriseUserDAO;


    @Override
    public Boolean audit(AuditCheckRequest request, LoginUserInfo user) {
        //稽核信息
        AuditStatusDO auditStatusDO = auditStatusMapper.selectByPrimaryKey(request.getCheckId());
        //线索审核信息
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(request.getLineId());
        String partnerId = lineInfo.getPartnerId();
        LineAuditInfoDO auditInfo = new LineAuditInfoDO();
        auditInfo.setLineId(request.getLineId());
        auditInfo.setPartnerId(partnerId);
        if (request.getCheckStatus().equals(0)) {
            auditInfo.setResultType(AuditResultTypeEnum.PASS.getCode());
            auditInfo.setPassReason(request.getCheckResult());
        } else if (request.getCheckStatus().equals(1)) {
            auditInfo.setResultType(AuditResultTypeEnum.REJECT.getCode());
            auditInfo.setRejectPublicReason(request.getCheckResult());
            auditInfo.setRejectRealReason(request.getCheckResult());
        }
        Long auditId = lineAuditInfoDAO.addAuditInfo(auditInfo);
        auditStatusDO.setAuditId(auditId);
        auditStatusDO.setAuditUserId(Long.valueOf(user.getUserId()));
        auditStatusDO.setAuditUserName(user.getName());
        auditStatusDO.setAuditStatus(request.getCheckStatus());
        auditStatusDO.setUpdateTime(DateUtils.getNowDate());
        auditStatusMapper.updateByPrimaryKeySelective(auditStatusDO);
        return true;
    }

    @Override
    public int insert(Long lineId, Long interviewId, Long auditId, Integer auditStage) {
        AuditStatusDO auditStatusDO = new AuditStatusDO();
        auditStatusDO.setLineId(lineId);
        auditStatusDO.setInterviewId(interviewId);
        auditStatusDO.setAuditId(auditId);
        auditStatusDO.setAuditStage(auditStage);
        return auditStatusMapper.insertSelective(auditStatusDO);
    }


    @Override
    public AuditDetailResponse detail(AuditDetailRequest request) {
        AuditDetailResponse response = new AuditDetailResponse();
        AuditStatusDO auditStatusDO = auditStatusMapper.selectByPrimaryKey(request.getCheckId());
        LineInterviewDO lineInterviewDO = lineInterviewMapper.selectByPrimaryKey(auditStatusDO.getInterviewId());
        LineAuditInfoDO auditInfo = lineAuditInfoDAO.getAuditInfo(auditStatusDO.getAuditId());
        response.setVideoUrl(JSONObject.parseArray(lineInterviewDO.getVideoUrl(), String.class));
        if (Objects.nonNull(lineInterviewDO.getActualStartTime())) {
            response.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, lineInterviewDO.getActualStartTime()));
        }
        if (Objects.nonNull(lineInterviewDO.getActualEndTime())) {
            response.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, lineInterviewDO.getActualEndTime()));
        }
        if (Objects.nonNull(auditStatusDO.getUpdateTime())) {
            response.setCheckTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, auditStatusDO.getUpdateTime()));
        }
        response.setCheckUser(auditStatusDO.getAuditUserName());
        response.setCheckStatus(auditStatusDO.getAuditStatus());
        if (auditInfo.getResultType() > 0) {
            response.setCheckResult(auditInfo.getRejectPublicReason());
        } else {
            response.setCheckResult(auditInfo.getPassReason());
        }

        return response;
    }

    @Override
    public PageInfo<CheckListResponse> checkList(String partnerNameOrPhone,
                                                 String startTime,
                                                 String endTime,
                                                 String region,
                                                 Integer checkStatus,
                                                 Integer checkStage,
                                                 Integer pageNum,
                                                 Integer pageSize) {
        if (Objects.nonNull(region)) {
            HyOpenAreaInfoDO areaInfo = hyOpenAreaInfoDAO.selectById(Long.valueOf(region));
            if (Objects.nonNull(areaInfo)) {
                region = areaInfo.getAreaPath();
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CheckListResponse> checkListResponses = auditStatusMapper.checkList(partnerNameOrPhone, startTime, endTime, region, checkStatus, checkStage, pageNum, pageSize);
        //意向区域
        List<Long> wantRegions = checkListResponses.stream().map(CheckListResponse::getWantRegion).collect(Collectors.toList());
        //面审人
        List<String> intervieweeIds = checkListResponses.stream().map(CheckListResponse::getIntervieweeId).collect(Collectors.toList());
        //招商经理
        List<String> investmentIds = checkListResponses.stream().map(CheckListResponse::getInvestmentId).collect(Collectors.toList());
        //意向区域Map
        Map<Long, String> wantShopAreaMap = hyOpenAreaInfoDAO.selectNameMapByIds(wantRegions);
        //招商经理Map
        Map<String, String> investmentIdMap = enterpriseUserDAO.getUserNameMap(investmentIds);
        //面审人Map
        Map<String, String> intervieweeIdMap = enterpriseUserDAO.getUserNameMap(intervieweeIds);

        checkListResponses.forEach(x -> {
            x.setIntervieweeName(intervieweeIdMap.get(x.getIntervieweeId()));
            x.setWantRegionName(wantShopAreaMap.get(x.getWantRegion()));
            x.setInvestmentName(investmentIdMap.get(x.getInvestmentId()));
        });
        return new PageInfo<>(checkListResponses);
    }
}
