/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.dispatch.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.dispatch.dto.*;
import com.tofly.dispatch.service.DispatchOrderReplyService;
import com.tofly.dispatch.service.DispatchOrderService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * 指令管理
 *
 * @author admin
 * @date Fri Nov 11 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dispatchorder")
@Api(tags="指令管理接口")
public class DispatchOrderController {

    private final  DispatchOrderService dispatchOrderService;


    private final DispatchOrderReplyService dispatchOrderReplyService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param dispatchOrderQuery 指令管理
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询所有指令数据")
    public ResultRespone getDispatchOrderPage(Page page, @ModelAttribute DispatchOrderQuery dispatchOrderQuery) {
      return ResultRespone.success(dispatchOrderService.getPageData(page, dispatchOrderQuery));
    }


    /**
     * 分页查询
     * @param page 分页对象
     * @param dispatchOrderQuery 指令管理
     * @return
     */
    @GetMapping("/userPage" )
    @ApiOperation(value = "分页查询当前用户的指令数据列表")
    public ResultRespone getDispatchOrderUserPage(Page page, @ModelAttribute DispatchOrderQuery dispatchOrderQuery) {
        dispatchOrderQuery.setCreateUser(SecurityUtils.getUserId());

        return ResultRespone.success(dispatchOrderService.getPageData(page, dispatchOrderQuery));
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param dispatchOrderQuery 指令管理
     * @return
     */
    @GetMapping("/acceptPage" )
    @ApiOperation(value = "分页查询可以操作（接受/拒接）指令的数据列表")
    public ResultRespone getDispatchOrderAcceptPage(Page page, @ModelAttribute DispatchOrderQuery dispatchOrderQuery) {
        dispatchOrderQuery.setDepartmentId(SecurityUtils.getUserDeptId());

        return ResultRespone.success(dispatchOrderService.getPageData(page, dispatchOrderQuery));
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param dispatchOrderQuery 指令管理
     * @return
     */
    @GetMapping("/replyOrderPage" )
    @ApiOperation(value = "分页查询可以进行回复的指令列表")
    public ResultRespone getDispatchOrderReplyPage(Page page, @ModelAttribute DispatchOrderQuery dispatchOrderQuery) {
        dispatchOrderQuery.setDepartmentId(SecurityUtils.getUserDeptId());
        dispatchOrderQuery.setOrderState("1");
        return ResultRespone.success(dispatchOrderService.getPageData(page, dispatchOrderQuery));
    }


    /**
     * 指令回复
     * @param dispatchOrderReplyDto 指令回复
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增指令回复" )
    @ApiOperation(value = "新增指令回复")
    @PostMapping("/saveReply")
    public ResultRespone saveReply(@Validated DispatchOrderReplyDto dispatchOrderReplyDto,
                              @ApiParam("文件附件列表") List<MultipartFile> fileList) {
        return ResultRespone.success(dispatchOrderReplyService.OrderReply(dispatchOrderReplyDto,fileList,true));
    }


    /**
     * 修改指令回复
     * @param dispatchOrderReplyDto 指令回复
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改指令回复" )
    @ApiOperation(value = "修改指令回复")
    @PutMapping("/updateReply")
    public ResultRespone updateReply(@Validated DispatchOrderReplyDto dispatchOrderReplyDto,
                                    @ApiParam("文件附件列表") List<MultipartFile> fileList) {
        return ResultRespone.success(dispatchOrderReplyService.OrderReply(dispatchOrderReplyDto,fileList,false));
    }

    /**
     * 通过id查询指令管理
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(dispatchOrderService.getById(id));
    }


    /**
     * 新增指令
     * @param dispatchOrderDto 指令管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增指令" )
    @ApiOperation(value = "新增指令")
    @PostMapping("/saveOrder")
    public ResultRespone saveOrder(@Validated DispatchOrderDto dispatchOrderDto,
                             @ApiParam("文件附件列表") List<MultipartFile> fileList) {
        return ResultRespone.success(dispatchOrderService.saveOrder(dispatchOrderDto,fileList,true));
    }


    /**
     * 新增修改
     * @param dispatchOrderDto 指令管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改指令" )
    @ApiOperation(value = "修改指令")
    @PostMapping("/updateOrder")
    public ResultRespone updateOrder(@Validated DispatchOrderDto dispatchOrderDto,
                              @ApiParam("文件附件列表") List<MultipartFile> fileList) {
        if(StringUtil.isNull(dispatchOrderDto.getId())){
            return ResultRespone.failed("参数id不能为空");
        }

        return ResultRespone.success(dispatchOrderService.saveOrder(dispatchOrderDto,fileList,false));
    }


    /**
     * 新增指令管理
     * @param dispatchOrderSendDto 指令管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="指令发送" )
    @ApiOperation(value = "指令发送")
    @PostMapping("/sendOrder")
    public ResultRespone sendOrder(@Validated DispatchOrderSendDto dispatchOrderSendDto) {

        return ResultRespone.success(dispatchOrderService.sendOrder(dispatchOrderSendDto));
    }


    /**
     * 指令接收
     * @param ids 指令接收
     * @return ResultRespone
     */
    @ToFlyAppLog(title="指令接受/拒接" )
    @ApiOperation(value = "指令接受/拒接")
    @PostMapping("/acceptOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量接受，多个id以逗号隔开", required = true, dataType = "String"),
            @ApiImplicitParam(name = "orderState", value = "接受状态，1已接受、2已拒绝", required = true, dataType = "String"),
            @ApiImplicitParam(name = "refuseReason", value = "指令拒绝接受原因，已拒绝时候必填", required = false, dataType = "String")

    })
    public ResultRespone acceptOrder(String ids,String orderState,String refuseReason) {

        return ResultRespone.success(dispatchOrderService.acceptOrder(ids,orderState,refuseReason));
    }

    /**
     * 指令完成
     * @param ids 指令完成
     * @return ResultRespone
     */
    @ToFlyAppLog(title="指令完成" )
    @ApiOperation(value = "指令完成")
    @PostMapping("/finishOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量指令完成，多个id以逗号隔开", required = true, dataType = "String")
    })
    public ResultRespone acceptOrder(String ids) {

        return ResultRespone.success(dispatchOrderService.acceptOrder(ids,"3",""));
    }


    /**
     * 通过id批量删除指令管理
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除指令管理" )
    @ApiOperation(value = "通过ID批量删除指令管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(dispatchOrderService.deleteByIds(ids));
    }
}
