package org.linlinjava.jz.admin.web;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.core.validator.Order;
import org.linlinjava.jz.core.validator.Sort;
import org.linlinjava.jz.db.domain.JzTicketRelation;
import org.linlinjava.jz.dto.TicketAction;
import org.linlinjava.jz.dto.TicketAllinone;
import org.linlinjava.jz.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Api(value = "Admin-Ticket-Controller", description = "工单管理")
@RestController
@RequestMapping("/admin/ticket")
@Validated
public class AdminTicketController {
    private final Log logger = LogFactory.getLog(AdminTicketController.class);

    @Autowired
    private TicketService ticketService;

    /**
     * @param mobile     客户手机
     * @param tab        前端tab值 1:我的工单 2：24小时工单 3：48小时 4：已完结
     * @param ticketNo   工单编号
     * @param ticketType 工单类型 0咨询 1投诉
     * @param status     工单状态
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:ticket:listAll")
    @RequiresPermissionsDesc(menu = {"工单管理", "全部工单"}, button = "查询")
    @GetMapping("/listAll")
    public Object listAll(@RequestParam(required = false) String mobile,
                          @NotNull @RequestParam Integer tab,
                          @RequestParam(required = false) String ticketNo,
                          @RequestParam(required = false) Integer orderId,
                          @RequestParam(required = false) Integer ticketType,
                          @RequestParam(required = false) Integer status,
                          @RequestParam(required = false) Integer subStatus,
                          @RequestParam(required = false) String startTime,
                          @RequestParam(required = false) String endTime,
                          @RequestParam(required = false) String updateUser,
                          @RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer limit,
                          @Sort @RequestParam(defaultValue = "create_time") String sort,
                          @Order @RequestParam(defaultValue = "desc") String order) {

        return ticketService.listAll(mobile, tab, ticketNo, orderId, ticketType, status, subStatus, startTime, endTime, updateUser, page, limit, sort, order);
    }

    /**
     * @param mobile     客户手机
     * @param tab        前端tab值 1:我的工单 2：24小时工单 3：48小时 4：已完结
     * @param ticketNo   工单编号
     * @param ticketType 工单类型 0咨询 1投诉
     * @param status     工单状态
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequiresPermissions("admin:ticket:list")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(required = false) String mobile,
                       @NotNull @RequestParam Integer tab,
                       @RequestParam(required = false) String ticketNo,
                       @RequestParam(required = false) Integer orderId,
                       @RequestParam(required = false) Integer ticketType,
                       @RequestParam(required = false) Integer status,
                       @RequestParam(required = false) Integer subStatus,
                       @RequestParam(required = false) String startTime,
                       @RequestParam(required = false) String endTime,
                       @RequestParam(required = false) String updateUser,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        return ticketService.list(SecurityHelper.getAdminId(), mobile, tab, ticketNo, orderId, ticketType, status, subStatus, startTime, endTime, updateUser, page, limit, sort, order);

    }

    @RequiresPermissions("admin:ticket:create")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody TicketAllinone allinone) {
        allinone.setAdminId(SecurityHelper.getAdminId());
        return ticketService.create(allinone);
    }

    @RequiresPermissions("admin:ticket:upgrade")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "流转")
    @PostMapping("/upgrade")
    public Object upgrade(@RequestBody TicketAction action) {
        action.setAdminId(SecurityHelper.getAdminId());
        return ticketService.upgrade(action);
    }

    @RequiresPermissions("admin:ticket:reject")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "驳回")
    @PostMapping("/reject")
    public Object reject(@RequestBody TicketAction action) {
        action.setAdminId(SecurityHelper.getAdminId());
        return ticketService.reject(action);
    }


    @RequiresPermissions("admin:ticket:completed")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "完成")
    @PostMapping("/completed")
    public Object completed(@RequestBody TicketAction action) {
        action.setAdminId(SecurityHelper.getAdminId());
        return ticketService.completed(action);
    }

    @RequiresPermissions("admin:ticket:memo")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "备注")
    @PostMapping("/memo")
    public Object remark(@RequestBody TicketAction action) {
        return ticketService.addMemo(action);
    }

    @RequiresPermissions("admin:ticket:detail")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "详情")
    @GetMapping("/detail")
    public Object detail(@NotNull @RequestParam Integer id) {
        return ticketService.detail(id);
    }

    @RequiresPermissions("admin:ticket:reopen")
    @RequiresPermissionsDesc(menu = {"工单管理", "我的工单"}, button = "重新打开")
    @PostMapping("/reopen")
    public Object reopen(@RequestBody TicketAction action) {
        action.setAdminId(SecurityHelper.getAdminId());
        return ticketService.reopen(action);
    }


    @Cacheable(cacheNames = "o2oCache", key = "'ticketResult'")
    @GetMapping("/ticketResult")
    @ApiOperation("工单处理结果枚举类")
    public Object ticketResult() {
        return ticketService.ticketResult();
    }


    @GetMapping("/selectTicketByMobile")
    public Object selectTicketByMobile(@NotBlank @RequestParam String mobile) {
        return ticketService.selectTicketByMobile(mobile);
    }

    /*添加话务记录于工单关系*/
    @PostMapping("/addRelation")
    public Object addRelation(@RequestBody JzTicketRelation relation){
        relation.setCreateUser(SecurityHelper.getAdminId());
        return ticketService.addRelation(relation);
    }

}
