package com.ruoyi.app.controller.drill;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.app.controller.common.AppBaseController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.AppUserTypeEnum;
import com.ruoyi.common.constant.DrillEnum;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.JWTUtil;
import com.ruoyi.drill.domain.DrillPlan;
import com.ruoyi.drill.dto.DrillPlanDto;
import com.ruoyi.drill.dto.DrillPlanListDto;
import com.ruoyi.drill.service.IDrillPlanService;
import com.ruoyi.system.mapper.SysRoleMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 演习计划Controller
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Api(tags = "演习计划")
@RestController
@RequestMapping("/app/drill/plan")
public class DrillPlanAppController extends AppBaseController {

    @Resource
    private IDrillPlanService drillPlanService;
    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询演习计划列表
     */
    @GetMapping("/list")
    @ApiOperation("查询演习计划列表")
    public AjaxResult list(DrillPlan drillPlan) {
        // 获取app账号对应的pc的userId
        String pcUserId = JWTUtil.getPCUserId();
        if (StringUtils.isEmpty(pcUserId)) {
            return success(new ArrayList<DrillPlanListDto>());
        }
        //设置组织人为当前登录人
        drillPlan.setDrillOrganizer(Long.parseLong(pcUserId));
        String requestToken = JWTUtil.getRequestToken();
        String appUserType = null;
        try {
            appUserType = JWTUtil.getAppUserType();
        } catch (Exception e) {

        }
        //承包商不让查看，直接返回空集合
        if (appUserType != null && AppUserTypeEnum.CONTRACTOR.getValue() == Integer.parseInt(appUserType)) {
            return success(new ArrayList<DrillPlanListDto>());
        }
        // APP请求时
        else if (com.ruoyi.common.utils.StringUtils.isNotBlank(requestToken)) {
            boolean leader = JWTUtil.isLeader();
            // 领导可查看全部
            if (leader) {
                drillPlan.setDrillOrganizer(null);
            }
        }


//        boolean leader = JWTUtil.isLeader();
//        List<Long> postIdList = JWTUtil.getPostIdList();

        // 领导可查看全部
   /*     List<SysRole> sysRoles = sysRoleMapper.selectRolePermissionByUserId(Long.valueOf(pcUserId));
        if (CollectionUtils.isNotEmpty(sysRoles)) {
            long count = sysRoles.stream().filter(li -> li.getRoleKey().equals(DrillEnum.DRILL_LEADER.getName())).count();

            if (count > 0) {
                drillPlan.setDrillOrganizer(null);
            }
        }*/
        return success(drillPlanService.findList(drillPlan));
    }

    /**
     * 获取演习计划详细信息
     * @param id 演习计划ID
     * @param drillStatus 演习状态：1-未完成；2-已完成(drill_plan)
     */
    @GetMapping(value = "/getInfo")
    @ApiOperation("获取演习计划详细信息")
    public AjaxResult getInfo(String id, Long drillStatus) {
        if (StringUtils.isBlank(id) || drillStatus == null) {
            return error("参数为空，请检查！");
        }

        if (drillStatus != DrillEnum.DRILL_STATUS_NO.getValue() && drillStatus != DrillEnum.DRILL_STATUS_YES.getValue()) {
            return error("参数错误，请检查！");
        }

        return drillPlanService.getInfo(id, drillStatus);
    }

    /**
     * 启动演习
     *
     * @param drillPlan
     * @return
     * @author matianxiang
     * @date 2023/8/9 15:38
     */
    @Log(title = "启动演习", businessType = BusinessType.UPDATE)
    @PostMapping("/startPlan")
    @ApiOperation("启动演习")
    public AjaxResult startPlan(@RequestBody DrillPlanDto drillPlan) {
        if (StringUtils.isEmpty(drillPlan.getId())) {
            return AjaxResult.error("id不能为空");
        }
        return drillPlanService.startPlanForApp(drillPlan);
    }
}
