package com.rf.psychological.plan.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.plan.service.PlanMessageService;
import com.rf.psychological.plan.service.TestPlanService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.Result;
import com.rf.psychological.plan.dao.model.PlanMessageEntity;
import com.rf.psychological.plan.dao.model.TestPlanEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author:zzf
 * @Date:2022/6/22:15:47
 * @Description:
 */
@Slf4j
@Api(tags = "测试计划提醒")
@RestController
@RequestMapping("/planMessage")
public class PlanMessageController extends BaseController {
    @Autowired
    private PlanMessageService planMessageService;
    @Autowired
    private TestPlanService testPlanService;
    @PostMapping("/save")
    @SafetyProcess
    @ApiOperation("保存测试计划短信提醒")
    public Result save(@RequestBody String json) throws Exception {
         JSONObject jsonObject = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data")));
        PlanMessageEntity planMessageEntity = jsonObject.toJavaObject(PlanMessageEntity.class);
        try {
            TestPlanEntity testPlanEntity = this.testPlanService.findById(planMessageEntity.getPlanId());
            if(testPlanEntity.getStatus() == Constant.TEST_PLAN_STATUS_STOP || testPlanEntity.getStatus() == Constant.TEST_PLAN_STATUS_END){
                return fail("测试计划"+testPlanEntity.getName()+"已结束，不能修改");
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(StringUtils.isNotBlank(planMessageEntity.getId())){
                if(new Date().after(simpleDateFormat.parse(planMessageEntity.getNoticeTime())) || planMessageEntity.getStatus().equals(Constant.MAGIC_NUM_1)){
                    return fail("序号"+planMessageEntity.getOrders()+"短信提醒已发送，不能修改");
                }
            }else{
                if(new Date().after(simpleDateFormat.parse(planMessageEntity.getNoticeTime())) || planMessageEntity.getStatus().equals(Constant.MAGIC_NUM_1)){
                    return fail("短信提醒时间早于当前时间");
                }
            }
            this.planMessageService.save(planMessageEntity);
        } catch (ParseException e) {
            e.printStackTrace();
            return fail("保存失败");
        }
        return success();
    }

    @DeleteMapping("/delete/{id}")
    @SafetyProcess
    @ApiOperation("删除测试计划短信提醒")
    public Result delete(@PathVariable String id) throws ParseException {
        PlanMessageEntity planMessageEntity = this.planMessageService.findById(id);
        if(planMessageEntity == null){
            return fail("操作的对象不存在");
        }
        try {
            TestPlanEntity testPlanEntity = this.testPlanService.findById(planMessageEntity.getPlanId());
            if(testPlanEntity.getStatus() == Constant.TEST_PLAN_STATUS_STOP || testPlanEntity.getStatus() == Constant.TEST_PLAN_STATUS_END){
               return fail("测试计划"+testPlanEntity.getName()+"已结束，不能修改");
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(planMessageEntity.getId() != null ){
                if(new Date().after(simpleDateFormat.parse(planMessageEntity.getNoticeTime())) || planMessageEntity.getStatus().equals(Constant.MAGIC_NUM_1)){
                    return fail("序号"+planMessageEntity.getOrders()+"短信提醒已发送，不能修改");
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return fail();
        }
        this.planMessageService.deleteById(id);
        return success();
    }

    @GetMapping("/find/{planId}")
    @SafetyProcess
    @ApiOperation("查询测试计划短信提醒")
    public Result find(@PathVariable String planId){
        return  success( this.planMessageService.findByPlanId(planId));
    }
}
