package org.linlinjava.jz.admin.web;

import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.util.ResponseUtil;
import org.linlinjava.jz.core.validator.Order;
import org.linlinjava.jz.core.validator.Sort;
import org.linlinjava.jz.db.domain.JzDmTicketResult;
import org.linlinjava.jz.db.service.JzDmTicketResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "Admin-DmTicketResult-Controller",description = "工单处理结果维护")
@RestController
@RequestMapping("/admin/ticketResult")
@Validated
public class AdminDmTicketResultController {

    private final Log logger = LogFactory.getLog(AdminDmTicketResultController.class);

    @Autowired
    private JzDmTicketResultService ticketResultService;


    @RequiresPermissions("admin:ticketResult:list")
    @RequiresPermissionsDesc(menu = {"工单管理", "处理结果"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzDmTicketResult> shopList = ticketResultService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(shopList);
    }

    @CacheEvict(cacheNames = "o2oCache", key = "'ticketResult'")
    @RequiresPermissions("admin:ticketResult:create")
    @RequiresPermissionsDesc(menu = {"工单管理", "处理结果"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzDmTicketResult result) {
        result.setCreateUser(SecurityHelper.getAdminId());
        result.setUpdateUser(SecurityHelper.getAdminId());
        result.setDeleted(false);
        ticketResultService.add(result);
        return ResponseUtil.ok(result);
    }

    @CacheEvict(cacheNames = "o2oCache", key = "'ticketResult'")
    @RequiresPermissions("admin:ticketResult:update")
    @RequiresPermissionsDesc(menu = {"工单管理", "处理结果"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzDmTicketResult result) {
        result.setUpdateUser(SecurityHelper.getAdminId());
        ticketResultService.updateById(result);
        return ResponseUtil.ok();
    }

    @CacheEvict(cacheNames = "o2oCache", key = "'ticketResult'")
    @RequiresPermissions("admin:ticketResult:delete")
    @RequiresPermissionsDesc(menu = {"工单管理", "处理结果"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzDmTicketResult result) {
        result.setUpdateUser(SecurityHelper.getAdminId());
        result.setDeleted(true);
        ticketResultService.updateById(result);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:ticketResult:all")
    @RequiresPermissionsDesc(menu = {"工单管理", "处理结果"}, button = "查询所有")
    @GetMapping("/all")
    public Object all() {
        return ResponseUtil.ok(ticketResultService.queryAll());
    }


}
