package com.hz.pm.api.projectdeclared.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.function.VUtils;
import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.statemachine.util.StateMachineUtil;
import com.hz.pm.api.projectdeclared.model.dto.OperationDTO;
import com.hz.pm.api.projectdeclared.model.entity.Operation;
import com.hz.pm.api.projectdeclared.model.vo.OperationVO;
import com.hz.pm.api.projectdeclared.service.IOperationService;
import com.hz.pm.api.projectlib.model.enumeration.ProjectStatusEnum;
import com.hz.pm.api.projectlib.model.entity.Project;
import com.hz.pm.api.projectlib.service.IProjectService;
import com.hz.pm.api.user.security.model.UserInfoDetails;
import com.hz.pm.api.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Classname OperationManage
 * @Description
 * @Date 2023/7/31 14:48
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class OperationManage {

    private final IOperationService operationService;

    private final IProjectService projectService;

    private final StateMachineUtil stateMachineUtil;

    /**
     * 获取实施详情
     * @param projectId
     * @return
     */
    public OperationVO detail(Long projectId) {
        Project project = projectService.getNewProject(projectId);
        VUtils.isTrue(Objects.isNull(project))
                .throwMessage("项目不存在!");

        Operation operation = operationService.getOne(Wrappers.lambdaQuery(Operation.class)
                .eq(Operation::getProjectCode, project.getProjectCode())
                .last(BizConst.LIMIT_1));

        return BeanUtil.copyProperties(operation,OperationVO.class);
    }

    public String pushOperation(OperationDTO operation) {
        UserInfoDetails user = LoginUserUtil.loginUserDetail();

        Project project = projectService.getNewProject(operation.getProjectId());
        VUtils.isTrue(Objects.isNull(project))
                .throwMessage("项目不存在!");

        //首先要判断 项目当前状态 是不是 待开工状态
        VUtils.isTrue(!ProjectStatusEnum.OPERATION.getCode().equals(project.getStatus()) ||
                        !ProjectStatusEnum.PROJECT_APPROVED.getCode().equals(project.getStage()))
                .throwMessage("提交失败 该项目不是 待开工或者已立项阶段");

        Operation old = operationService.getOne(Wrappers.lambdaQuery(Operation.class)
                .eq(Operation::getProjectCode, project.getProjectCode())
                .last(BizConst.LIMIT_1));

        Operation entity = BeanUtil.copyProperties(operation, Operation.class);
        if(Objects.nonNull(old)){
            entity.setId(old.getId());
        }else{
            entity.setCreateOn(LocalDateTime.now());
            entity.setCreateBy(user.getUsername());
        }
        entity.setProjectCode(project.getProjectCode());
        entity.setUpdateOn(LocalDateTime.now());
        entity.setUpdateBy(user.getUsername());
        //强制放入 计划终验时间
        entity.setFinalInspectionDate(project.getPlanAcceptanceTime());
        if(operationService.saveOrUpdate(entity)){
            stateMachineUtil.pass(project);
            project.setUpdateOn(LocalDateTime.now());
            projectService.updateById(project);
        }

        return entity.getProjectCode();
    }
}
