package com.yuchen.saas.park.service.service.work.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.manage.system.api.feign.IOrgUserClient;
import com.yuchen.saas.park.api.dto.work.SaasWorkOrderDelayApprovalDto;
import com.yuchen.saas.park.api.dto.work.SaasWorkOrderDelayApprovalQueryDto;
import com.yuchen.saas.park.api.dto.work.WorkOrderDelayApplyDto;
import com.yuchen.saas.park.api.entity.work.SaasWorkOrder;
import com.yuchen.saas.park.api.entity.work.SaasWorkOrderDelayApproval;
import com.yuchen.saas.park.api.vo.work.SaasWorkOrderDelayApprovalStatisticsVo;
import com.yuchen.saas.park.api.vo.work.SaasWorkOrderDelayApprovalVo;
import com.yuchen.saas.park.service.mapper.work.SaasWorkOrderDelayApprovalMapper;
import com.yuchen.saas.park.service.service.work.ISaasWorkOrderDelayApprovalService;
import com.yuchen.saas.park.service.service.work.ISaasWorkOrderService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.secure.utils.AuthUtil;
import org.nest.springwrap.core.tool.utils.BeanUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工单延迟审批表 服务实现类
 * </p>
 *
 * @author caiwq
 * @since 2024-04-07
 */
@Service
public class SaasWorkOrderDelayApprovalServiceImpl extends BaseServiceImpl<SaasWorkOrderDelayApprovalMapper, SaasWorkOrderDelayApproval> implements ISaasWorkOrderDelayApprovalService {

    @Resource
    private IOrgUserClient iOrgUserClient;
    @Resource
    private ISaasWorkOrderService workOrderService;

    @Override
    public Boolean delayApply(WorkOrderDelayApplyDto workOrderDelayApplyDto) {
        SaasWorkOrderDelayApproval delayApproval = BeanUtil.copyProperties(workOrderDelayApplyDto, SaasWorkOrderDelayApproval.class);
        String applyNo =  "YS" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + (int) ((Math.random() * 9 + 1) * 1000);
        delayApproval.setApplyNo(applyNo);
        delayApproval.setApplyTime(new Date());
        // 申请人
        Long applyUserId = AuthUtil.getUser().getOrgUserId();
        String applyUserName = iOrgUserClient.detail(applyUserId).getData().getName();
        delayApproval.setApplyUserId(applyUserId);
        delayApproval.setApplyUserName(applyUserName);
        return save(delayApproval);
    }

    @Override
    public Boolean delayApproval(SaasWorkOrderDelayApprovalDto workOrderDelayApprovalDto) {
        SaasWorkOrderDelayApproval delayApproval = getById(workOrderDelayApprovalDto.getId());
        delayApproval.setApprovalStatus(workOrderDelayApprovalDto.getApprovalStatus());
        delayApproval.setApprovalTime(new Date());
        // 审批人
        Long approvalUserId = AuthUtil.getUser().getOrgUserId();;
        String approvalUserName = iOrgUserClient.detail(approvalUserId).getData().getName();
        delayApproval.setApprovalUserId(approvalUserId);
        delayApproval.setApprovalUserName(approvalUserName);
        // 更新工单工单处理时限
        if (workOrderDelayApprovalDto.getApprovalStatus() == 1) {
            SaasWorkOrder workOrder = workOrderService.lambdaQuery().eq(SaasWorkOrder::getWorkOrderNo, delayApproval.getPgNo()).one();
            workOrder.setTimeLimitCount(workOrder.getTimeLimitCount() + delayApproval.getExtendHour());
            workOrderService.updateById(workOrder);
        }
        return updateById(delayApproval);
    }

    @Override
    public SaasWorkOrderDelayApprovalStatisticsVo statisticsAuditCount() {
        SaasWorkOrderDelayApprovalStatisticsVo statisticsVo = new SaasWorkOrderDelayApprovalStatisticsVo();
        Integer allCount = this.lambdaQuery().count();
        statisticsVo.setAllCount(allCount);
        Integer toConfirmCount = this.lambdaQuery().eq(SaasWorkOrderDelayApproval::getApprovalStatus, 0).count();
        statisticsVo.setToConfirmCount(toConfirmCount);
        Integer passedCount = this.lambdaQuery().eq(SaasWorkOrderDelayApproval::getApprovalStatus, 1).count();
        statisticsVo.setPassedCount(passedCount);
        Integer rejectedCount = this.lambdaQuery().eq(SaasWorkOrderDelayApproval::getApprovalStatus, 2).count();
        statisticsVo.setRejectedCount(rejectedCount);
        return statisticsVo;
    }

    @Override
    public NestPage<SaasWorkOrderDelayApprovalVo> list(SaasWorkOrderDelayApprovalQueryDto queryDto) {
        LambdaQueryWrapper<SaasWorkOrderDelayApproval> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Func.isNotEmpty(queryDto.getProjectId()), SaasWorkOrderDelayApproval::getProjectId, queryDto.getProjectId());
        queryWrapper.like(Func.isNotEmpty(queryDto.getPgNo()), SaasWorkOrderDelayApproval::getPgNo, queryDto.getPgNo());
        queryWrapper.like(Func.isNotEmpty(queryDto.getApplyUserName()), SaasWorkOrderDelayApproval::getApplyUserName, queryDto.getApplyUserName());
        queryWrapper.like(Func.isNotEmpty(queryDto.getApprovalUserName()), SaasWorkOrderDelayApproval::getApprovalUserName, queryDto.getApprovalUserName());
        queryWrapper.eq(Func.isNotEmpty(queryDto.getApprovalStatus()), SaasWorkOrderDelayApproval::getApprovalStatus, queryDto.getApprovalStatus());
        queryWrapper.ge(Func.isNotEmpty(queryDto.getApplyStartTime()), SaasWorkOrderDelayApproval::getApplyTime, queryDto.getApplyStartTime());
        queryWrapper.le(Func.isNotEmpty(queryDto.getApplyEndTime()), SaasWorkOrderDelayApproval::getApplyTime, queryDto.getApplyEndTime());
        queryWrapper.orderByDesc(SaasWorkOrderDelayApproval::getCreateTime);
        IPage<SaasWorkOrderDelayApproval> page = this.page(Condition.getPage(queryDto), queryWrapper);
        IPage<SaasWorkOrderDelayApprovalVo> result = new Page<>(queryDto.getCurrent(), queryDto.getSize());
        List<SaasWorkOrderDelayApproval> recordList = page.getRecords();
        List<SaasWorkOrderDelayApprovalVo> list = BeanUtil.copyProperties(recordList, SaasWorkOrderDelayApprovalVo.class);
        result.setRecords(list);
        result.setTotal(page.getTotal());
        return NestPage.of(result);
    }

}
