package com.sunda.spmsweb.ordercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IShoppingCartService;
import com.sunda.spmsweb.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车接口", description = "购物车接口")
public class ShoppingCartController {

    @Autowired
    IShoppingCartService iShoppingCartService;

    @RequestMapping("/getShoppingCartList")
    @ApiOperation(value = "获取指定工号购物车信息", notes = "获取指定工号购物车信息，参考ID：10000 或 10001", httpMethod = "POST")
    public ResponseResult getShoppingCartList(@RequestParam("userId") String userId){
        try{
            return ResponseResult.success().add("shoppingCartList", iShoppingCartService.getShoppingCartList(userId));
        } catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/getShoppingCartLists")
    @ApiOperation(value = "获取指定工号购物车信息-带全部长协", notes = "获取指定工号购物车信息，参考ID：20001\n" +
            "返回信息包含已选物料对应长协信息，和可选择的其他长协信息", httpMethod = "POST")
    public ResponseResult getShoppingCartLists(@RequestParam("userId") String userId){
        try{
            return ResponseResult.success().add("shoppingCartList", iShoppingCartService.getShoppingCartLists(userId));
        } catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @RequestMapping("/getCartList")
    @ApiOperation(value = "token获取当前人购物车信息", notes = "获取当前人购物车信息", httpMethod = "GET")
    public ResponseResult getCartList(){
        String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
        try{
            return ResponseResult.success().add("shoppingCartList", iShoppingCartService.getShoppingCartList(userId));
        } catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("addShoppingCart")
    @ApiOperation(value = "新增购物车数据", notes = "新增购物车数据，根据工号、物料编号查询购物车是否存在该条数据，存在则删除后新增数据，不存在则新增一条数据。\n" +
            "{\n" +
            "\t\"userId\": \"10001\",\n" +
            "\t\"shoppingCartList\": [{\n" +
            "\t\t\"materialNo\": \"120000028\",\n" +
            "\t\t\"quantity\": 10,\n" +
            "\t\t\"longContractCode\": \"10000000006\",\n" +
            "\t\t\"brand\": \"brand A\"\n" +
            "\t}, {\n" +
            "\t\t\"materialNo\": \"120000029\",\n" +
            "\t\t\"quantity\": 10,\n" +
            "\t\t\"longContractCode\": \"\",\n" +
            "\t\t\"brand\": \"\"\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    public ResponseResult addShoppingCart(@RequestBody JSONObject jsonObject){
        try {
            return iShoppingCartService.addShoppingCart(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/deleteShoppingCart")
    @ApiOperation(value = "删除购物车指定行数据", notes = "根据用户工号和物料编号删除购物车指定数据，quantity、sourceListId 可不传值。\n" +
            "{\n" +
            "\t\"userId\": \"10000\",\n" +
            "\t\"materialNo\": [\"210036864\", \"210023513\"]\n" +
            "}", httpMethod = "POST")
    public ResponseResult deleteShoppingCart(@RequestBody JSONObject jsonObject){
        try {
            return iShoppingCartService.deleteShoppingCart(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/updateShoppingCart")
    @ApiOperation(value = "整体更新购物车信息", notes = "根据物料编号和数量更新购物车信息，删除购物车原有数据整体更新.\n" +
            "{\n" +
            "\t\"userId\": \"10001\",\n" +
            "\t\"shoppingCartList\": [{\n" +
            "\t\t\"materialNo\": \"120000023\",\n" +
            "\t\t\"quantity\": 10,\n" +
            "\t\t\"longContractCode\": \"\",\n" +
            "\t\t\"brand\": \"\"\n" +
            "\t}, {\n" +
            "\t\t\"materialNo\": \"120000022\",\n" +
            "\t\t\"quantity\": 10,\n" +
            "\t\t\"longContractCode\": \"\",\n" +
            "\t\t\"brand\": \"\"\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    public ResponseResult updateShoppingCart(@RequestBody JSONObject jsonObject){
        try {
            return iShoppingCartService.updateShoppingCart(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error("请求异常请重试:" + e);
        }
    }


}
