package com.xunmei.core.resumption.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.constant.SecurityConstants;
import com.xunmei.common.core.domain.R;
import com.xunmei.common.core.web.domain.AjaxResult;
import com.xunmei.core.resumption.domain.AppPlan;
import com.xunmei.core.resumption.domain.AppPlanToItem;
import com.xunmei.core.resumption.service.AppPlanService;
import com.xunmei.core.resumption.vo.appPlan.AppPlanVo;
import com.xunmei.core.resumption.vo.appPlan.RuleItemVo;
import com.xunmei.core.resumption.vo.appPlan.RuleRequestVo;
import com.xunmei.system.api.RemoteOrgService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : 高雄
 * @date :2022/4/27 17:38
 * @description :
 * @modyified By:
 */
@RestController
@RequestMapping("/api/plan")
@Slf4j
public class ApiPlanController {

    @Autowired
    private AppPlanService appPlanService;
    @Autowired
    private RemoteOrgService remoteOrgService;

//    @Autowired
//    ResumptionTaskBusiness taskBusiness;

    /**
     * 计划分页查询
     * @param info
     * @return
     */
    @PostMapping("/find")
    public AjaxResult hostList(@RequestBody AppPlanVo info){
        Page<AppPlan> result = appPlanService.selectPage(info);
        return AjaxResult.success(result);
    }
    @RequestMapping("/test")
    public AjaxResult test(Integer type){
        R<List<Long>> res = remoteOrgService.findListByOrgType(type, SecurityConstants.INNER);
        List<Long> list=null;
        if (res.getCode()==200) {
            list = res.getData();
        }
        return AjaxResult.success(list);
    }

    /**
     * 检查要点分页查询
     * @return Message
     * @author 高雄
     * @time 2022/5/5 14:39
     */
    @RequestMapping("/item/find")
    public AjaxResult itemList(@RequestBody RuleItemVo info){
        Page<RuleItemVo> result = appPlanService.selectItemPage(info);
        return AjaxResult.success(result);
    }

    /**
     * 获取机构或者机构类型对应的检查规范
     * @param info
     * @return Message
     * @author 高雄
     * @time 2022/5/5 15:05
     */
    @RequestMapping("/find/rule")
    public AjaxResult findRule(@RequestBody RuleRequestVo info){
        List<Map<String, Object>> list = appPlanService.findByOrg(info);
        return AjaxResult.success(list);
    }

    /**
     * 新增 修改履职计划
     * @param info
     * @return Message
     * @author 高雄
     * @time 2022/5/5 16:37
     */
    @RequestMapping("/edit")
    public AjaxResult edit(@RequestBody AppPlanVo info){
        try {
            appPlanService.saveOrUpdatePlan(info);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("数据处理错误："+ e.getMessage());
        }
    }


    /**
     * 获取详情数据
     * @param id
     * @return Message
     * @author 高雄
     * @time 2022/5/5 17:38
     */
    @ApiOperation(value = "获取详情")
    @GetMapping(value = "/find/{id}")
    public AjaxResult findById(@PathVariable  Long id){
        AppPlanVo app = appPlanService.findById(id);
        return AjaxResult.success(app);
    }

    @ApiOperation(value = "获取详情")
    @GetMapping(value = "/getPointIds/{id}")
    public AjaxResult getPointIds(@PathVariable  Long id){
        return AjaxResult.success(appPlanService.selectItemIdsByPlanId(id));
    }

    /**
     * 获取详情数据
     * @param id
     * @return Message
     * @author 高雄
     * @time 2022/5/5 17:38
     */
    @ApiOperation(value = "删除计划")
    @GetMapping(value = "/delete/{id}")
    public AjaxResult deleted(@PathVariable  Long id){
        try {
            appPlanService.deleted(id);
            return AjaxResult.success("数据处理成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("数据处理失败," + e.getMessage());
        }

    }


}