package com.gree.mall.miniapp.logic.worker;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.bean.worker.TeamCount;
import com.gree.mall.miniapp.bean.worker.WebsitWorkerBean;
import com.gree.mall.miniapp.enums.ExamineStatusEnum;
import com.gree.mall.miniapp.enums.StateEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.User;
import com.gree.mall.miniapp.plus.entity.WebsitUser;
import com.gree.mall.miniapp.plus.entity.WorkerTeam;
import com.gree.mall.miniapp.plus.entity.WorkerTeamApply;
import com.gree.mall.miniapp.plus.service.UserService;
import com.gree.mall.miniapp.plus.service.WebsitUserService;
import com.gree.mall.miniapp.plus.service.WorkerTeamApplyService;
import com.gree.mall.miniapp.plus.service.WorkerTeamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class WorkerTeamLogic {

    @Resource
    WorkerTeamService workerTeamService;
    @Resource
    WorkerTeamApplyService workerTeamApplyService;
    @Resource
    CommonLogic commonLogic;
    @Resource
    UserService userService;
    @Resource
    WebsitUserService websitUserService;


    public IPage<WorkerTeamApply> page(Integer pageNum, Integer pageSize) {
        CurrentCompanyWechat current = commonLogic.getCurrentCompanyWechat();
        final Page<WorkerTeamApply> page = workerTeamApplyService.lambdaQuery()
                .eq(WorkerTeamApply::getCompanyWechatId, current.getUser().getCompanyWechatId())
                .and(v -> v.eq(WorkerTeamApply::getMasterWorkerId, current.getUser().getWorkerNumber())
                        .or()
                        .eq(WorkerTeamApply::getAssistantWorkerId, current.getUser().getWorkerNumber())
                )
                .page(new Page<>(pageNum, pageSize));
        return page;
    }

    public IPage<WorkerTeam> managePage(Integer pageNum, Integer pageSize) {
        CurrentCompanyWechat current = commonLogic.getCurrentCompanyWechat();
        final Page<WorkerTeam> page = workerTeamService.lambdaQuery()
                .eq(WorkerTeam::getCompanyWechatId, current.getUser().getCompanyWechatId())
                .and(v -> v.eq(WorkerTeam::getMasterWorkerId, current.getUser().getWorkerNumber())
                        .or()
                        .eq(WorkerTeam::getAssistantWorkerId, current.getUser().getWorkerNumber())
                )
                .page(new Page<>(pageNum, pageSize));
        return page;
    }


    public void add(WorkerTeamApply workerTeam) {
        CurrentCompanyWechat current = commonLogic.getCurrentCompanyWechat();
        workerTeam.setCompanyWechatId(current.getUser().getCompanyWechatId())
                .setCompanyWechatName(current.getUser().getCompanyName());
        if (StringUtils.isBlank(workerTeam.getWebsitId())) {
            throw new RemoteServiceException("网点编号不能为空");
        }
        if (StringUtils.isBlank(workerTeam.getMasterWorkerId()) || StringUtils.isBlank(workerTeam.getMasterWorkerName())) {
            workerTeam.setMasterWorkerId(current.getUser().getWorkerNumber())
                    .setMasterWorkerName(current.getUser().getNickName());
        }
        if (StringUtils.isBlank(workerTeam.getAssistantWorkerId()) || StringUtils.isBlank(workerTeam.getAssistantWorkerName())) {
            throw new RemoteServiceException("组队师傅不能为空");
        }
        final Integer count = workerTeamService.lambdaQuery()
                .eq(WorkerTeam::getWebsitId, workerTeam.getWebsitId())
                .eq(WorkerTeam::getMasterWorkerId, workerTeam.getMasterWorkerId())
                .eq(WorkerTeam::getAssistantWorkerId, workerTeam.getAssistantWorkerId())
                .count();
        if (count > 0) {
            throw new RemoteServiceException("已和 " + workerTeam.getAssistantWorkerName() + " 师傅组队");
        }
        workerTeam.insert();
    }

    public void confirm(String id, ExamineStatusEnum status) {
        CurrentCompanyWechat current = commonLogic.getCurrentCompanyWechat();
        final WorkerTeamApply apply = workerTeamApplyService.getById(id);
        if (status.getKey().equals(ExamineStatusEnum.OK.getKey())) {
            new WorkerTeam()
                    .setCompanyWechatId(apply.getCompanyWechatId())
                    .setCompanyWechatName(apply.getCompanyWechatName())
                    .setWebsitId(apply.getWebsitId())
                    .setMasterWorkerId(apply.getMasterWorkerId())
                    .setMasterWorkerName(apply.getMasterWorkerName())
                    .setAssistantWorkerId(apply.getAssistantWorkerId())
                    .setAssistantWorkerName(apply.getAssistantWorkerName())
                    .setStatus(StateEnum.ON.getKey())
                    .insert();
        } else {
            apply.setStatus(status.getKey())
                    .updateById();
        }
    }

    public List<WebsitWorkerBean> queryWebsitWorker(String websitId) {
        CurrentCompanyWechat current = commonLogic.getCurrentCompanyWechat();
        final List<WebsitUser> websitUsers = websitUserService.lambdaQuery()
                .eq(WebsitUser::getCompanyWechatId, current.getUser().getCompanyWechatId())
                .eq(WebsitUser::getWebsitId, websitId)
                .ne(WebsitUser::getUserId, current.getUserId())
                .eq(WebsitUser::getExamineStatus, ExamineStatusEnum.OK.getKey())
                .list();
        final List<User> userList = userService.lambdaQuery()
                .eq(User::getCompanyWechatId, current.getUser().getCompanyWechatId())
                .in(User::getUserId, websitUsers.stream().map(WebsitUser::getUserId).collect(Collectors.toList()))
                .list();
        final Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getWorkerNumber, User::getNickName));
        List<WebsitWorkerBean> beanList = new ArrayList<>();
        for (WebsitUser websitUser : websitUsers) {
            if (userMap.containsKey(websitUser.getWorkerNumber())) {
                WebsitWorkerBean bean = new WebsitWorkerBean();
                bean.setWorkerId(websitUser.getWorkerNumber());
                bean.setWorkerName(userMap.get(websitUser.getWorkerNumber()));
                beanList.add(bean);
            }
        }
        return beanList;
    }


    public TeamCount count() {
        CurrentCompanyWechat current = commonLogic.getCurrentCompanyWechat();
        final Integer count1 = workerTeamApplyService.lambdaQuery()
                .eq(WorkerTeamApply::getCompanyWechatId, current.getUser().getCompanyWechatId())
                .and(v -> v.eq(WorkerTeamApply::getMasterWorkerId, current.getUser().getWorkerNumber())
                        .or()
                        .eq(WorkerTeamApply::getAssistantWorkerId, current.getUser().getWorkerNumber())
                )
                .count();

        final Integer count2 = workerTeamService.lambdaQuery()
                .eq(WorkerTeam::getCompanyWechatId, current.getUser().getCompanyWechatId())
                .and(v -> v.eq(WorkerTeam::getMasterWorkerId, current.getUser().getWorkerNumber())
                        .or()
                        .eq(WorkerTeam::getAssistantWorkerId, current.getUser().getWorkerNumber())
                )
                .count();

        TeamCount count = new TeamCount();
        count.setApplyCount(count1);
        count.setTeamCount(count2);
        return count;
    }
}
