package com.kmall.admin.controller;

import com.kmall.admin.entity.CommentEntity;
import com.kmall.admin.service.CommentService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-08-28 17:03:40
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("comment:list")
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        String valueName = (String) params.get("valueName");
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(valueName)){
            try{
                valueName = new String(valueName.getBytes("iso-8859-1"),"utf-8");
            }catch (Exception e){
                e.printStackTrace();
            }
            params.put("valueName", valueName);
        }
        String userName = (String) params.get("userName");
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(userName)){
            try{
                userName = new String(userName.getBytes("iso-8859-1"),"utf-8");
            }catch (Exception e){
                e.printStackTrace();
            }
            params.put("userName", userName);
        }
        //查询列表数据
        Query query = new Query(params);

        List<CommentEntity> commentList = commentService.queryList(query);
        int total = commentService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("comment:info")
    public R info(@PathVariable("id") Integer id) {
        CommentEntity comment = commentService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("comment:save")
    public R save(@RequestBody CommentEntity comment) {
        commentService.save(comment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("comment:update")
    public R update(@RequestBody CommentEntity comment) {
        commentService.update(comment);

        return R.ok();
    }

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

        return R.ok();
    }

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

        List<CommentEntity> list = commentService.queryList(params);

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

    /**
     * 修改状态
     */
    @RequestMapping("/toggleStatus")
    @RequiresPermissions("comment:toggleStatus")
    public R toggleStatus(@RequestBody CommentEntity comment) {
        commentService.toggleStatus(comment);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/toggleStatusBatch")
    @RequiresPermissions("comment:toggleStatus")
    public R toggleStatusBatch(@RequestBody Integer[] ids) {
        commentService.toggleStatusBatch(ids);

        return R.ok();
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = commentService.queryTotal(params);

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