package com.kmall.admin.controller;

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

import com.kmall.admin.entity.SysCusUnitCodeEntity;
import com.kmall.admin.service.SysCusUnitCodeService;
import com.kmall.admin.fromcomm.entity.SysUserEntity;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
import com.kmall.admin.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 huangyq
 * @email admin@qhdswl.com
 * @date 2018-11-10 15:34:36
 */
@Controller
@RequestMapping("syscusunitcode")
public class SysCusUnitCodeController {
    @Autowired
    private SysCusUnitCodeService sysCusUnitCodeService;

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

        List<SysCusUnitCodeEntity> sysCusUnitCodeList = sysCusUnitCodeService.queryList(query);
        int total = sysCusUnitCodeService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{sn}")
    @RequiresPermissions("syscusunitcode:info")
    @ResponseBody
    public R info(@PathVariable("sn") String sn) {
        SysCusUnitCodeEntity sysCusUnitCode = sysCusUnitCodeService.queryObject(sn);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("syscusunitcode:save")
    @ResponseBody
    public R save(@RequestBody SysCusUnitCodeEntity sysCusUnitCode) {

        SysUserEntity user = ShiroUtils.getUserEntity();
        sysCusUnitCode.setCreaterSn(user.getUserId().toString());
        sysCusUnitCode.setCreateTime(new Date());
        sysCusUnitCode.setModerSn(user.getUserId().toString());
        sysCusUnitCode.setModTime(new Date());

        sysCusUnitCodeService.save(sysCusUnitCode);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("syscusunitcode:update")
    @ResponseBody
    public R update(@RequestBody SysCusUnitCodeEntity sysCusUnitCode) {

        SysUserEntity user = ShiroUtils.getUserEntity();
        sysCusUnitCode.setModerSn(user.getUserId().toString());
        sysCusUnitCode.setModTime(new Date());

        sysCusUnitCodeService.update(sysCusUnitCode);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("syscusunitcode:delete")
    @ResponseBody
    public R delete(@RequestBody String[]sns) {
        sysCusUnitCodeService.deleteBatch(sns);

        return R.ok();
    }

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

        List<SysCusUnitCodeEntity> list = sysCusUnitCodeService.queryList(params);

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