/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/23
 */
package org.springblade.modules.business.equipment.inspection.plan.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.InspectionStatusEnum;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.business.equipment.inspection.base.dto.EquipmentInspectionInfoDTO;
import org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity;
import org.springblade.modules.business.equipment.inspection.base.service.IEquipmentInspectionService;
import org.springblade.modules.business.equipment.inspection.base.vo.EquipmentInspectionInfoVO;
import org.springblade.modules.business.equipment.inspection.plan.dto.EquipmentInspectionPlanDTO;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.equipment.inspection.plan.vo.EquipmentInspectionPlanVO;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/***
 * Date:2022/8/23
 * Title: 巡检汛期时间配置
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/equipment/inspection/plan")
@AllArgsConstructor
@Api(value = "设备巡检汛期管理模块", tags = "设备巡检汛期管理模块")
public class EquipmentInspectionPlanController extends BladeController {
    private final IEquipmentInspectionPlanService planService;
    private final IEquipmentInspectionService inspectionService;

    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入 EquipmentInspectionPlanDTO 实体类")
    public R<IPage<EquipmentInspectionPlanVO>> page(@ApiIgnore EquipmentInspectionPlanDTO inspectionInfoDTO, Query query) {

        IPage<EquipmentInspectionPlanVO> pages = planService.selectPage(Condition.getPage(query), inspectionInfoDTO);
        return R.data(pages);
    }


    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R<EquipmentInspectionPlanEntity> detail() {
        BladeUser user = AuthUtil.getUser();
        LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
        wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0);
        wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
        List<EquipmentInspectionPlanEntity> list = planService.list(wrapper);
        if (null != list && list.size() > 0) {
            EquipmentInspectionPlanEntity entity = list.get(0);
            return R.data(entity);
        } else {
            EquipmentInspectionPlanEntity entity = new EquipmentInspectionPlanEntity();
            entity.setId(0L);
            entity.setCreateDept(Func.toLong(user.getDeptId()));
            entity.setCreateUser(user.getUserId());
            entity.setCreateTime(new Date());
            entity.setStatus(1);
            entity.setIsDeleted(0);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.MONTH, 4 - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            entity.setBeforeRainSeasonStartTime(calendar.getTime());
            calendar.set(Calendar.MONTH, 6 - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            entity.setBeforeRainSeasonEndTime(calendar.getTime());
            calendar.set(Calendar.MONTH, 6 - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            entity.setRainSeasonFirstStartTime(calendar.getTime());
            calendar.set(Calendar.MONTH, 8 - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 15);
            entity.setRainSeasonFirstEndTime(calendar.getTime());
            calendar.set(Calendar.MONTH, 8 - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 15);
            entity.setRainSeasonSecondStartTime(calendar.getTime());
            calendar.set(Calendar.MONTH, 10 - 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            entity.setRainSeasonSecondEndTime(calendar.getTime());
            return R.data(entity);
        }
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R save(@RequestBody EquipmentInspectionPlanEntity planEntity) {
        BladeUser user = AuthUtil.getUser();
        planEntity.setPlanStatus(1);
        planEntity.setOrgId(Func.toLong(user.getDeptId()));
        planService.save(planEntity);
        if (planEntity.getPlanScheduledType() == 1) {
            //执行周期为仅一次时，立即生成巡检任务
            EquipmentInspectionInfoEntity inspectionInfoEntity = new EquipmentInspectionInfoEntity();
            inspectionInfoEntity.setInspectionPlanId(planEntity.getId());
            inspectionInfoEntity.setInspectionTitle(planEntity.getPlanTitle());
            inspectionInfoEntity.setInspectionStatus(InspectionStatusEnum.STATUS_TODO.getCode());
            inspectionInfoEntity.setOrgId(Func.toLong(user.getDeptId()));
            inspectionService.save(inspectionInfoEntity);
        }
        return R.status(true);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入planEntity")
    public R update(@RequestBody EquipmentInspectionPlanEntity planEntity) {
        return R.status(planService.updateById(planEntity));
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入planEntity")
    public R submit(@RequestBody EquipmentInspectionPlanEntity planEntity) {
        if (planEntity.getId() <= 0L) {
            planEntity.setId(null);
        }
        return R.status(planService.saveOrUpdate(planEntity));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入projectInfoEntity")
    public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
        boolean temp = planService.deleteLogic(Func.toLongList(ids));
        return R.status(temp);
    }
}
