package com.pj.tb_orders_cart;

import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.tb_orders_cart.vo.OrderListVo;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * Controller: tb_orders_cart -- 订单购物车
 *
 * @author linbl
 */
@RestController
@RequestMapping("/app/TbOrdersCart/")
public class TbOrdersCartAppController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbOrdersCartService tbOrdersCartService;

    /**
     * 增
     */
    @RequestMapping("add")
    public AjaxJson add(@Validated TbOrdersCart t) {
        t.setUserId(StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        TbOrdersCart check = tbOrdersCartService.selectByOrderIdAndUserId(t);
        if (check == null) {
            t.setCreateTime(new Date());
            return AjaxJson.toAjax(tbOrdersCartService.save(t));
        } else {
            return AjaxJson.getError("该订单已添加");
        }
    }

    /**
     * 清理购物车,可批量
     */
    @RequestMapping("deleteByIds/{ids}")
    public AjaxJson deleteByIds(@PathVariable Long[] ids) {
        return AjaxJson.getSuccessData(tbOrdersCartService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 查集合 - 根据条件（参数为空时代表忽略指定条件）
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("userId", StpAPPUserUtil.getAPPLoginInfo().getLoginId());
        List<OrderListVo> list = tbOrdersCartService.getOrderList(so);
        return AjaxJson.getSuccessData(list);
    }
}
