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.entity.PlatformScript;
import org.jeecg.modules.orderplatform.service.IPlatformMaterialService;
import org.jeecg.modules.orderplatform.service.IPlatformOrderService;
import org.jeecg.modules.orderplatform.util.ApprovalNodeNoticeHandler;
import org.jeecg.modules.orderplatform.util.DictHandler;
import org.jeecg.modules.orderplatform.util.NewAndOldInteract;
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;
import java.util.Map;

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

    @Autowired
    private IPlatformMaterialService platformMaterialService;

    @Autowired
    private IPlatformOrderService platformOrderService;

    @Autowired
    private OperateRecordHandler operateRecordHandler;

    @Autowired
    private ApprovalNodeNoticeHandler approvalNodeNoticeHandler;

    @Autowired
    private DictHandler dictHandler;

    @Autowired
    private NewAndOldInteract  newAndOldInteract;

    /**
     * 根据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;
    }

    /**
     * 脚本是否可更新
     */
    @GetMapping(value = "/verifyScript")
    public Result<Object> verifyScript(String materialCode, String scriptId) {
        Result<Object> result = new Result<>();
        QueryWrapper<PlatformMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_code", materialCode);
        PlatformMaterial platformMaterial = platformMaterialService.getOne(queryWrapper);
        if (platformMaterial.getScriptId().equals(scriptId)) {
            result.setSuccess(true);
        } else {
            result.error500("脚本信息已被更改，请刷新后操作");
        }
        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");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_MATERIAL("ACCEPT_SCRIPT",platformMaterial.getMaterialCode());
            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");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_MATERIAL("REJECT_SCRIPT",platformMaterial.getMaterialCode());
            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;
    }

    /**
     * 视频是否可以更新
     */
    @GetMapping(value = "/verifyVideo")
    public Result<Object> verifyVideo(String materialCode, String videoId) {
        Result<Object> result = new Result<>();
        QueryWrapper<PlatformMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_code", materialCode);
        PlatformMaterial platformMaterial = platformMaterialService.getOne(queryWrapper);
        if (platformMaterial.getVideoId().equals(videoId)) {
            result.setSuccess(true);
        } else {
            result.error500("视频信息已被更改，请刷新后操作");
        }
        return result;
    }

    /**
     * 运营审核视频
     */
    @PostMapping(value = "/video/accept")
    public Result<Object> acceptVideo(@RequestBody PlatformMaterial platformMaterial) {
        Result<Object> result = new Result<>();
        //视频通过同步到素材库
        boolean sync = platformMaterialService.syncVideo2CTopMaterial(platformMaterial);
        if (!sync) {
            result.error500("视频同步素材库失败");
            return 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");
                approvalNodeNoticeHandler.NOTICE_WE_CHAT_MATERIAL("ACCEPT_VIDEO",platformMaterial.getMaterialCode());
                //todo new->old
                newAndOldInteract.NEW_2_OLD(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");
            approvalNodeNoticeHandler.NOTICE_WE_CHAT_MATERIAL("REJECT_VIDEO",platformMaterial.getMaterialCode());
            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;
    }

    /**
     * 视频修改回显
     */
    @GetMapping(value = "/videoEcho")
    public Result<Map<String, Object>> videoEcho(String videoId) {
        Result<Map<String, Object>> result = new Result<>();
        Map<String, Object> scriptDetail = platformMaterialService.videoEcho(videoId);
        result.setResult(scriptDetail);
        result.setSuccess(true);
        return result;
    }

}
