package org.jeecg.modules.orderplatform.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.orderplatform.entity.PlatformMaterial;
import org.jeecg.modules.orderplatform.service.IPlatformMaterialService;
import org.jeecg.modules.orderplatform.service.IPlatformOrderService;
import org.jeecg.modules.orderplatform.util.DictHandler;
import org.jeecg.modules.orderplatform.util.OperateRecordHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 物料审批流程
 */
@Slf4j
@RestController
@RequestMapping("/platform/material")
public class PlatformMaterialCtrl {

    @Autowired
    private IPlatformMaterialService platformMaterialService;

    @Autowired
    private IPlatformOrderService platformOrderService;

    @Autowired
    private OperateRecordHandler operateRecordHandler;

    @Autowired
    DictHandler dictHandler;

    /**
     * 根据orderCode查询关联物料
     */
    @GetMapping(value = "/listByOrderCode")
    public Result<PageInfo<PlatformMaterial>> listByOrderCode(PlatformMaterial platformMaterial,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        Result<PageInfo<PlatformMaterial>> result = new Result<>();
        QueryWrapper<PlatformMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(platformMaterial);
        PageHelper.startPage(pageNo,pageSize);
        List<PlatformMaterial> platformMaterialList= platformMaterialService.list(queryWrapper);
        result.setSuccess(true);
        result.setResult(new PageInfo<>(dictHandler.dictHandlerMaterial(platformMaterialList)));
        return result;
    }

    /**
     * 根据设计查询我参与的物料列表
     */
    @GetMapping(value = "/listByRole")
    public Result<PageInfo<JSONObject>> listByRole(@RequestParam String designUserId,
                                                   @RequestParam String materialStatus,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<PageInfo<JSONObject>> result = new Result<>();
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> jsonObjectList = platformMaterialService.queryMaterialsByDesignUserId(materialStatus, designUserId);
        result.setResult(new PageInfo<>(jsonObjectList));
        result.setSuccess(true);
        return result;
    }

    /**
     * 运营查询需要自己审批的订单
     */
    @GetMapping(value = "/listByOperatorUserId")
    public Result<PageInfo<JSONObject>> listByOperatorUserId(@RequestParam String operatorUserId,
                                                             @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                             @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<PageInfo<JSONObject>> result = new Result<>();
        PageHelper.startPage(pageNo, pageSize);
        List<JSONObject> jsonObjectList = platformMaterialService.queryMaterialsByOperatorUserId(operatorUserId);
        result.setResult(new PageInfo<>(jsonObjectList));
        result.setSuccess(true);
        return result;
    }


    /**
     * 运营审核脚本
     */
    @PostMapping(value = "/script/accept")
    public Result<Object> acceptScript(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        //物料状态->脚本通过，视频待上传
        platformMaterial.setMaterialStatus(4);
        platformMaterial.setScriptStatus(3);
        platformMaterial.setVideoStatus(0);
        boolean ok = platformMaterialService.updateById(platformMaterial);
        if (ok) {
            operateRecordHandler.Approval(platformMaterial.getMaterialCode(),"ACCEPT_SCRIPT");
            result.success("脚本审核通过!");
        } else {
            result.error500("脚本审核失败!请联系管理员");
        }
        return result;
    }

    //驳回脚本，驳回理由必填
    @PostMapping(value = "/script/reject")
    public Result<Object> rejectScript(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        //物料状态->脚本被驳回，可以重新提交
        platformMaterial.setMaterialStatus(3);
        platformMaterial.setScriptStatus(2);
        boolean ok = platformMaterialService.updateById(platformMaterial);
        if (ok) {
            operateRecordHandler.Approval(platformMaterial.getMaterialCode(),"REJECT_SCRIPT");
            result.success("脚本已驳回!");
        } else {
            result.error500("脚本驳回失败!请联系管理员");
        }
        return result;
    }

    /**
     * 脚本详情页
     */
    @GetMapping(value = "/scriptDetail")
    public Result<JSONObject> scriptDetail(String scriptId) {
        Result<JSONObject> result = new Result<>();
        JSONObject scriptDetail = platformMaterialService.queryScriptDetailById(scriptId);
        result.setResult(scriptDetail);
        result.setSuccess(true);
        return result;
    }

    /**
     * 运营审核视频
     */
    @PostMapping(value = "/video/accept")
    public Result<Object> acceptVideo(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        //物料状态->视频审核通过
        platformMaterial.setMaterialStatus(7);
        platformMaterial.setVideoStatus(3);
        boolean ok = platformMaterialService.updateById(platformMaterial);
        if (ok) {
            //更新订单的素材完成数
            boolean order = platformOrderService.updateOrderFinishAmount(platformMaterial.getOrderCode());
            if (order) {
                operateRecordHandler.Approval(platformMaterial.getMaterialCode(),"ACCEPT_VIDEO");
                //修改素材库审核状态
                platformMaterialService.updateMaterialStatusById(platformMaterial.getVideoId());
                result.success("视频审核通过!");
            }
        } else {
            result.error500("视频审核失败!请联系管理员");
        }
        return result;
    }

    //驳回视频，驳回理由必填
    @PostMapping(value = "/video/reject")
    public Result<Object> rejectVideo(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        //物料状态->视频被驳回，可以重新提交
        platformMaterial.setMaterialStatus(6);
        platformMaterial.setVideoStatus(2);
        boolean ok = platformMaterialService.updateById(platformMaterial);
        if (ok) {
            operateRecordHandler.Approval(platformMaterial.getMaterialCode(),"REJECT_VIDEO");
            result.success("视频已驳回!");
        } else {
            result.error500("视频驳回失败!请联系管理员");
        }
        return result;
    }

    /**
     * 视频详情页
     */
    @GetMapping(value = "/videoDetail")
    public Result<JSONObject> videoDetail(String videoId) {
        Result<JSONObject> result = new Result<>();
        JSONObject scriptDetail = platformMaterialService.queryVideoDetailById(videoId);
        result.setResult(scriptDetail);
        result.setSuccess(true);
        return result;
    }

}
