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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.worker.WorkerElecConfirmManualVO;
import com.gree.mall.manager.bean.worker.WorkerElecConfirmVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.constant.Constant;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.enums.WorkerElecSingConfirmEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.SysDictCompany;
import com.gree.mall.manager.plus.entity.User;
import com.gree.mall.manager.plus.entity.WorkerSignConfirm;
import com.gree.mall.manager.plus.entity.WorkerSignConfirmManual;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.utils.RedisUtil;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Slf4j
@Service
@RequiredArgsConstructor
public class WorkerElecConfirmLogic {

    private final CommonLogic commonLogic;
    private final CommonMapper commonMapper;
    private final WorkerSignConfirmService workerSignConfirmService;
    private final WorkerSignConfirmManualService workerSignConfirmManualService;
    private final WebsitUserService websitUserService;
    private final UserService userService;
    private final SysDictCompanyService sysDictCompanyService;
    private final RedisUtil redisUtil;

    public IPage<WorkerElecConfirmVO> list(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
//        String status = null;
//        if (CollectionUtil.isNotEmpty(zfireParamBean.getParams())) {
//            List<QueryParamBean> newParam = new ArrayList<>();
//            for (QueryParamBean param : zfireParamBean.getParams()) {
//                if (!param.getParam().equals("a.status")) {
//                    newParam.add(param);
//                }
//                status = param.getValue().toString();
//            }
//        }
        adminUser.setAdminCompanyWechat(null);
        FieldUtils.supplyParam(zfireParamBean, WorkerElecConfirmVO.class, adminUser);

        return commonMapper.workerSignConfirmPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

    }

    public WorkerElecConfirmVO detail(String userId, String belongCompanyCode) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        WorkerElecConfirmVO vo = new WorkerElecConfirmVO();
        final SysDictCompany company = sysDictCompanyService.lambdaQuery()
//                .eq(SysDictCompany::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(SysDictCompany::getDictType, "BELONG_COMPANY")
                .eq(SysDictCompany::getDictCode, belongCompanyCode)
                .one();
        final User user = userService.lambdaQuery()
//                .eq(User::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(User::getUserId, userId)
                .one();
        vo.setBelongCompany(company.getDictValue());
        vo.setBelongCompanyCode(belongCompanyCode);
        vo.setNickName(user.getNickName());
        vo.setMobile(user.getMobile());
        vo.setIdCard(user.getIdCard());

        final WorkerSignConfirm confirm = workerSignConfirmService.lambdaQuery()
//                .eq(WorkerSignConfirm::getCompanyWechatId, adminUser.getAdminCompanyWechat().getCompanyWechatId())
                .eq(WorkerSignConfirm::getUserId, userId)
                .eq(WorkerSignConfirm::getBelongCompanyCode, belongCompanyCode)
                .one();
        vo.setStatus(Objects.isNull(confirm) ? WorkerElecSingConfirmEnum.NO : WorkerElecSingConfirmEnum.YES);
        vo.setCreateTime(Objects.isNull(confirm) ? null : confirm.getCreateTime());
        vo.setUrl(Objects.isNull(confirm) ? null : confirm.getUrl());
        return vo;
    }

    public Integer count() {
        return commonMapper.workerSignConfirmCount();
    }

    public IPage<WorkerElecConfirmManualVO> manualList(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        FieldUtils.materialParam(zfireParamBean, WorkerElecConfirmManualVO.class, adminUser);

        return commonMapper.workerSignConfirmManualPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

    }

    public WorkerSignConfirmManual manualDetail(String id) {
        return workerSignConfirmManualService.getById(id);
    }

    @Transactional
    public void manualConfirm(String id, ExamineStatusEnum status) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        final WorkerSignConfirmManual manual = workerSignConfirmManualService.getById(id);

        final Integer count = workerSignConfirmService.lambdaQuery()
                .eq(WorkerSignConfirm::getCompanyWechatId, manual.getCompanyWechatId())
                .eq(WorkerSignConfirm::getBelongCompanyCode, manual.getBelongCompanyCode())
                .eq(WorkerSignConfirm::getUserId, manual.getUserId())
                .count();

        if (count > 0) {
            throw new RemoteServiceException(manual.getNickName() + "师傅签名记录已存在");
        }

        manual.setConfirmBy(adminUser.getNickName())
                .setConfirmTime(DateUtil.date())
                .setStatus(status.getKey())
                .updateById();

        if (status.getKey().equals(ExamineStatusEnum.OK.getKey())) {
            WorkerSignConfirm confirm = new WorkerSignConfirm();
            confirm.setCompanyWechatId(manual.getCompanyWechatId())
                    .setBelongCompanyCode(manual.getBelongCompanyCode())
                    .setBelongCompanyName(manual.getBelongCompanyName())
                    .setUserId(manual.getUserId())
                    .setWorkerNumber(manual.getWorkerNumber())
                    .setName(manual.getName())
                    .setUrl(manual.getUrl())
                    .insert();
        }

        redisUtil.del(Constant.RedisPrefix.WORKER_SIGN_CONFIRM + manual.getUserId());
    }

    public Integer manualCount() {
        return workerSignConfirmManualService.lambdaQuery()
                .eq(WorkerSignConfirmManual::getStatus, ExamineStatusEnum.WAIT.getKey())
                .count();
    }
}
