package jnpf.cultivate.controller.app.identify;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.constant.MsgCode;
import jnpf.cultivate.service.CultivateIdentifyApplyService;
import jnpf.enums.cultivate.ApplySourceEnum;
import jnpf.model.cultivate.dto.identify.*;
import jnpf.model.cultivate.vo.identify.*;
import jnpf.personnels.service.FtbPersonnelsPermissionsService;
import jnpf.util.FtbUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * app实操鉴定
 *
 * @author mouzeping
 */
@Slf4j
@RestController
@RequestMapping(value = "/cultivate/apply/app")
public class CultivateIdentifyAppController {
    @Resource
    private CultivateIdentifyApplyService applyService;
    @Resource
    private FtbPersonnelsPermissionsService ftbPersonnelsPermissionsService;

    /**
     * app/列表
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "列表")
    @GetMapping(value = "/list")
    public ActionResult<PageListVO<IdentifyApplyListAppVo>> getPageList(@Valid IdentifyApplyListAppDto req) {
        try {
            log.info("app/列表，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            PageInfo<IdentifyApplyListAppVo> page = applyService.getPageAppList(req);
            long ent = System.currentTimeMillis();
            log.info("app/列表，耗时=>{}", ent - st + " 毫秒");
            PaginationVO pagination = FtbUtil.getPagination(page);
//            pagination.setTotal(applyService.getPageAppListCount(req));
            return ActionResult.page(page.getList(), pagination);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * app/清空-清空全部
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "清空-清空全部")
    @DeleteMapping(value = "/deleteIds")
    public ActionResult deleteIds(@Valid @RequestBody IdentifyApplyDeleteAppDto req) {
        try {
            log.info("app/清空-清空全部，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            applyService.deleteIds(req);
            long ent = System.currentTimeMillis();
            log.info("app/清空-清空全部，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(MsgCode.SU003.get());
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * app/鉴定详情
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "鉴定详情")
    @GetMapping(value = "/identifyInfo")
    public ActionResult<IdentifyInfoAppVo> getApplyIdentifyInfoApp(@Valid IdentifyTableInfoDto req) {
        try {
            log.info("app/鉴定详情，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            IdentifyInfoAppVo infoAppVo = applyService.getIdentifyApplyInfoApp(req);
            long ent = System.currentTimeMillis();
            log.info("app/鉴定详情，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(infoAppVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * app/鉴定项详情
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "鉴定项详情")
    @GetMapping(value = "/identifyItemsInfo")
    public ActionResult<IdentifyItemsInfoAppVo> getApplyIdentifyItemsInfoApp(@Valid IdentifyTableInfoDto req) {
        try {
            log.info("app/鉴定项详情，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            IdentifyItemsInfoAppVo infoAppVo = applyService.getApplyIdentifyItemsInfoApp(req);
            long ent = System.currentTimeMillis();
            log.info("app/鉴定项详情，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(infoAppVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * app/鉴定提交
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "鉴定提交")
    @PostMapping(value = "/submit")
    public ActionResult submit(@Valid @RequestBody IdentifyApplySubmitDto req) {
        try {
            applyService.applyDataSubmit(req);
            return ActionResult.success(MsgCode.SU005.get());
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * app/设置鉴定时间
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "设置鉴定时间")
    @PutMapping(value = "/setTime")
    public ActionResult setTime(@Valid @RequestBody IdentifyApplySetTimeAppDto req) {
        try {
            log.info("app/设置鉴定时间，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            applyService.setApplyDataTime(req);
            long ent = System.currentTimeMillis();
            log.info("app/设置鉴定时间，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(MsgCode.SU005.get());
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * app/鉴定结果详情
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "鉴定结果详情")
    @GetMapping(value = "/basicInfo")
    public ActionResult<IdentifyApplyBasicInfoAppVo> getApplyBasicInfoApp(@Valid IdentifyTableInfoDto req) {
        try {
            log.info("app/鉴定结果详情，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            IdentifyApplyBasicInfoAppVo basicInfoAppVo = applyService.getApplyBasicInfoApp(req);
            long ent = System.currentTimeMillis();
            log.info("app/鉴定结果详情，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(basicInfoAppVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }


    /**
     * app/鉴定项结果详情
     *
     * @param req 帅选条件
     * @return
     * @
     */
    @Operation(summary = "鉴定项结果详情")
    @GetMapping(value = "/info")
    public ActionResult<IdentifyApplyInfoAppVo> getApplyInfoApp(@Valid IdentifyTableInfoDto req) {
        try {
            log.info("app/鉴定项结果详情，入参=>{}", JSONObject.toJSON(req));
            long st = System.currentTimeMillis();
            IdentifyApplyInfoAppVo infoAppVo = applyService.getApplyInfoApp(req);
            long ent = System.currentTimeMillis();
            log.info("app/鉴定项结果详情，耗时=>{}", ent - st + " 毫秒");
            return ActionResult.success(infoAppVo);
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * 本人申请实操鉴定
     *
     * @param req 入参
     * @return {@link ActionResult}
     */
    @PostMapping("/my-own-practical-appraisal")
    public ActionResult iApplyForPracticalAppraisal(@Valid @RequestBody IdentifyApplySaveDto req) {
        req.setSource(ApplySourceEnum.APPLY_IN_PERSON.getCode());
        applyService.applyDataSave(req);
        return ActionResult.success();
    }

    /**
     * 根据组织获取具有鉴定权限的鉴定人
     *
     * @param orgId 组织 ID
     * @return {@link ActionResult }<>
     */
    @GetMapping("/query-authority-appraiser")
    public ActionResult<List<IdentifyAppAuthorityAppraiserVO>> queryAuthorityAppraiser(@RequestParam(value = "orgId") String orgId) {
        List<IdentifyAppAuthorityAppraiserVO> result = ftbPersonnelsPermissionsService.queryAuthorityAppraiser(orgId);
        return ActionResult.success(result);
    }

    /**
     * b本地test打开可见性
     * @param userId
     * @param postId
     * @return
     */
    @GetMapping("/applyService")
    public ActionResult<Void> applyService(@RequestParam(value = "userId") String userId,
                                           @RequestParam(value = "postId") String postId ) {
        applyService.turnOnVisibility(postId, userId);
        return ActionResult.success();
    }
}
