package com.kmall.admin.controller;

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

import com.kmall.admin.entity.GoodsEntity;
import com.kmall.admin.entity.OfflineCartEntity;
import com.kmall.admin.service.GoodsService;
import com.kmall.admin.service.OfflineCartService;
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 emato
 * @email admin@qhdswl.com
 * @date 2018-11-29 15:14:59
 */
@Controller
@RequestMapping("offlinecart")
public class OfflineCartController {
    @Autowired
    private OfflineCartService offlineCartService;
    @Autowired
    private GoodsService goodsService;

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

        List<OfflineCartEntity> offlineCartList = offlineCartService.queryList(query);
        int total = offlineCartService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("offlinecart:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        OfflineCartEntity offlineCart = offlineCartService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("offlinecart:save")
    @ResponseBody
    public R save(@RequestBody OfflineCartEntity offlineCart) {
        offlineCartService.save(offlineCart);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("offlinecart:update")
    @ResponseBody
    public R update(@RequestBody OfflineCartEntity offlineCart) {
        offlineCartService.update(offlineCart);

        return R.ok();
    }

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

        return R.ok();
    }

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

        List<OfflineCartEntity> list = offlineCartService.queryList(params);

        return R.ok().put("list", list);
    }
    @RequestMapping("/infoList")
    @RequiresPermissions("offlinecart:infoList")
    @ResponseBody
    public R infoList(@RequestBody List<OfflineCartEntity> offlineCartEntityList) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        offlineCartService.deleteByUserId(user.getUserId());
        List<OfflineCartEntity> cartEntityList = new ArrayList<>();
        for (OfflineCartEntity cartEntity:offlineCartEntityList) {
            GoodsEntity goods = goodsService.queryObjectByProdBarcodeAndBizType(cartEntity.getProdBarcode(), user.getStoreId());
            cartEntityList = offlineCartService.offlineGoodsCart(goods);
        }

        return R.ok().put("list", cartEntityList);
    }
    /**
     * 删除
     */
    @RequestMapping("/deleteByUserId")
    @RequiresPermissions("offlinecart:deleteByUserId")
    @ResponseBody
    public R deleteByUserId() {
        SysUserEntity user = ShiroUtils.getUserEntity();
        offlineCartService.deleteByUserId(user.getUserId());
        return R.ok();
    }
}
