package com.fzm.mall.server.admin.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.order.entity.dto.ComplaintDTO;
import com.fzm.mall.server.admin.order.entity.dto.ComplaintSaveDTO;
import com.fzm.mall.server.admin.order.entity.dto.ComplaintUpdateDTO;
import com.fzm.mall.server.admin.order.service.IComplaintService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 订单中心-订单投诉 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-16
 */
@Api(tags = "订单中心")
@RestController
@RequestMapping("/complaint/complaintPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ComplaintController {

    private final IComplaintService complaintService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "订单中心-订单投诉-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "订单中心-订单投诉-新增", module = OperateModuleEnum.ORDER_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid ComplaintSaveDTO complaintSaveDTO) {

        complaintService.save(complaintSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "订单中心-订单投诉-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "订单中心-订单投诉-编辑", module = OperateModuleEnum.ORDER_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid ComplaintUpdateDTO complaintUpdateDTO,
                             @ApiParam(value = "管理员id", required = true) @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        complaintUpdateDTO.setHandlerId(admin.getMerchantId());

        complaintService.update(complaintUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "订单中心-订单投诉-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{complaintId}")
    public ResponseVO<ComplaintDTO> read(
            @ApiParam(value = "complaintId", required = true) @PathVariable(value = "complaintId") String complaintId) {
        ComplaintDTO result = complaintService.read(complaintId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "订单中心-订单投诉-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "用户id") @RequestParam(required = false) String uid,
                           @ApiParam(value = "投诉编号") @RequestParam(required = false) String complaintId,
                           @ApiParam(value = "投诉状态(1->处理中;2->已完成)") @RequestParam(required = false) Integer complaintStatus,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<ComplaintDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        IPage<ComplaintDTO> result = complaintService.page(page, uid, complaintId, complaintStatus);
        return resFac.getSuccessResponseWithData(result);
    }
}
