package com.kmall.admin.controller;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.HelpIssueEntity;
import com.kmall.admin.service.HelpIssueService;
import com.kmall.common.constant.Dict;
import com.kmall.common.entity.SysUserEntity;
import com.kmall.common.utils.*;
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.*;

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

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-12-01 21:09:54
 */
@Controller
@RequestMapping("helpissue")
public class HelpIssueController {
    @Autowired
    private HelpIssueService helpIssueService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("helpissue:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if (user != null) {
            if (Dict.roleType.item_2.getItem().equals(user.getRoleType())) {
                params.put("storeId", user.getStoreId());
                params.put("merchSn", user.getMerchSn());
            } else if (Dict.roleType.item_3.getItem().equals(user.getRoleType())) {
                params.put("merchSn", user.getMerchSn());
            }
        }
        //查询列表数据
        Query query = new Query(params);

        List<HelpIssueEntity> helpIssueList = helpIssueService.queryList(query);
        int total = helpIssueService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("helpissue:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        HelpIssueEntity helpIssue = helpIssueService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("helpissue:save")
    @ResponseBody
    public R save(@RequestBody HelpIssueEntity helpIssue) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(helpIssue);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        builder.put("typeId", "问题分类");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        helpIssueService.save(helpIssue);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("helpissue:update")
    @ResponseBody
    public R update(@RequestBody HelpIssueEntity helpIssue) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(helpIssue);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        builder.put("typeId", "问题分类");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        helpIssueService.update(helpIssue);

        return R.ok();
    }

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

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<HelpIssueEntity> list = helpIssueService.queryList(params);

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