package com.kmall.admin.controller;

import java.util.List;
import java.util.Map;

import com.kmall.admin.entity.OrderRefundEntity;
import com.kmall.admin.service.OrderRefundService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.constant.Dict;
import com.kmall.common.entity.SysUserEntity;
import com.kmall.common.service.SysUserService;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
import com.kmall.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 订单退款记录表Controller
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2018-11-01 10:16:41
 */
@Controller
@RequestMapping("orderrefund")
public class OrderRefundController {
    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("orderrefund:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
        //查询列表数据
        Query query = new Query(params);

        List<OrderRefundEntity> orderRefundList = orderRefundService.queryList(query);
        for (OrderRefundEntity entity: orderRefundList
             ) {
            if(entity.getIsOnfflineOrder().equalsIgnoreCase(Dict.isOnfflineOrder.item_1.getItem())) {
                SysUserEntity sysUserEntity = sysUserService.queryObject(entity.getUserId().longValue());
                entity.setUserName("操作店员:"+sysUserEntity.getUsername());
            }
        }
        int total = orderRefundService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(orderRefundList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("orderrefund:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        OrderRefundEntity orderRefund = orderRefundService.queryObject(id);

        if(orderRefund.getIsOnfflineOrder().equalsIgnoreCase(Dict.isOnfflineOrder.item_1.getItem())) {
            SysUserEntity sysUserEntity = sysUserService.queryObject(orderRefund.getUserId().longValue());
            orderRefund.setUserName("操作店员:"+sysUserEntity.getUsername());
        }
        return R.ok().put("orderRefund", orderRefund);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("orderrefund:save")
    @ResponseBody
    public R save(@RequestBody OrderRefundEntity orderRefund) {
        orderRefundService.save(orderRefund);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("orderrefund:update")
    @ResponseBody
    public R update(@RequestBody OrderRefundEntity orderRefund) {
        orderRefundService.update(orderRefund);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("orderrefund:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]ids) {
        orderRefundService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);
        List<OrderRefundEntity> list = orderRefundService.queryList(params);

        return R.ok().put("list", list);
    }
}
