package com.liquidnet.client.admin.web.controller.zhengzai.kylin;

import com.github.pagehelper.PageInfo;
import com.liquidnet.client.admin.common.annotation.Log;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.enums.BusinessType;
import com.liquidnet.client.admin.zhengzai.kylin.service.impl.KylinOrderRefundReasonAdminServiceImpl;
import com.liquidnet.service.kylin.dto.param.OrderRefundReasonParam;
import com.liquidnet.service.kylin.dto.param.OrderRefundReasonSearchParam;
import com.liquidnet.service.kylin.dto.vo.OrderRefundReasonVo;
import com.liquidnet.service.kylin.entity.KylinOrderRefundReasons;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 退款原因管理
 */
@Controller
@RequestMapping("/kylin/refundReasons")
public class KylinOrderRefundReasonsAdminController extends BaseController {

    private String prefix = "zhengzai/kylin/refundReasons";

    @Autowired
    private KylinOrderRefundReasonAdminServiceImpl refundReasonAdminService;

    @RequiresPermissions("kylin:refundReasons:list")
    @GetMapping()
    public String list() {
        return prefix + "/refundReasons";
    }

    @RequiresPermissions("kylin:refundReasons:list")
    @PostMapping("list")
    @ResponseBody
    public TableDataInfo listData(OrderRefundReasonSearchParam param) {
        PageInfo<KylinOrderRefundReasons> result = refundReasonAdminService.reasonList(param);
        return getDataTable(result.getList());
    }

    @GetMapping("/create")
    public String create() {
        return prefix + "/create";
    }

    @Log(title = "创建退款原因", businessType = BusinessType.INSERT)
    @RequiresPermissions("kylin:refundReasons:create")
    @PostMapping("create")
    @ResponseBody
    public AjaxResult createSave(OrderRefundReasonParam param) {
        Boolean res = refundReasonAdminService.create(param);
        return res ? success() : error("添加退款原因失败");
    }

    @GetMapping("/update/{reasonId}")
    public String update(@PathVariable("reasonId") String reasonId, ModelMap mmap) {
        OrderRefundReasonVo vo = refundReasonAdminService.detail(reasonId);
        if (vo == null || (vo.getIsApplied() != null && vo.getIsApplied() == 1)) {
            mmap.put("errorMsg", "应用中的退款原因不可编辑，请先取消应用");
        }
        mmap.put("RefundReasonVo", vo);
        return prefix + "/update";
    }

    @Log(title = "修改退款原因", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kylin:refundReasons:update")
    @PostMapping("update")
    @ResponseBody
    public AjaxResult updateSave(OrderRefundReasonParam param) {
        Boolean res = refundReasonAdminService.update(param);
        return res ? success() : error("修改失败（应用中不可编辑，或数据不存在）");
    }

    @Log(title = "删除退款原因", businessType = BusinessType.DELETE)
    @RequiresPermissions("kylin:refundReasons:delete")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(OrderRefundReasonParam param) {
        List<String> ids = param.getIds();
        Boolean result = refundReasonAdminService.delete(ids);
                return result ? success("删除成功") : error("删除失败（应用中的原因需先取消应用，或数据不存在）");
    }

    @Log(title = "应用/取消应用退款原因", businessType = BusinessType.UPDATE)
    @RequiresPermissions("kylin:refundReasons:apply")
    @PutMapping("apply")
    @ResponseBody
    public AjaxResult apply(@RequestParam("reasonId") String reasonId,
                            @RequestParam("isApplied") Integer isApplied) {
        Boolean result = refundReasonAdminService.apply(reasonId, isApplied);
        return result ? success() : error("操作失败");
    }

    /**
     * 已应用列表（供申请页下拉，走缓存）
     */
    @RequiresPermissions(value = {"kylin:refund:apply", "kylin:refundBatch:apply", "kylin:refundReasons:list"}, logical = Logical.OR)
    @GetMapping("appliedList")
    @ResponseBody
    public AjaxResult appliedList() {
        List<OrderRefundReasonVo> list = refundReasonAdminService.listApplied();
        return AjaxResult.success(list);
    }
}
