package com.qs.mp.web.controller.api.channel.mall;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qs.mp.channel.domain.ChannelCart;
import com.qs.mp.channel.domain.param.ChannelCartParam;
import com.qs.mp.channel.domain.vo.ChannelCartVO;
import com.qs.mp.channel.service.IChannelCartService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.domain.param.BatchLongIdsParam;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1/mp/channel/mall")
@Api(tags = "渠道盲票商城购物车接口")
@AllArgsConstructor
public class ChannelCartController extends BaseApiController {


  @Autowired
  private IChannelCartService channelCartService;

  @Autowired
  private MapperFacade mapperFacade;

  /**
   * 购物车列表
   */
  @PostMapping("/cart/list")
  @ApiOperation(value = "购物车列表" , notes = "获取所有购物车盲票信息")
  public AjaxResult list(@RequestBody ChannelCart param) {
    Long channelId = SecurityUtils.getLoginUser().getChannelId();
    startPage();
    List<ChannelCartVO> channelCartVOS = channelCartService.listCartVO(
        new LambdaQueryWrapper<ChannelCart>().eq(ChannelCart::getChannelId, channelId)
            .orderByDesc(ChannelCart::getUpdatedTime));
    return AjaxResult.success(channelCartVOS);
  }


  /**
   * 盲票列表页面点加购
   */
  @PostMapping("/cart/add")
  @ApiOperation(value = "加购物车" , notes = "渠道经销商加购物车")
  public AjaxResult add(@RequestBody ChannelCartParam param) {
    boolean rtn = channelCartService.addToCart(param);
    if (!rtn) {
      return AjaxResult.error("操作失败");
    }
    return AjaxResult.success("操作成功");
  }

  /**
   * 修改购物车数量
   */
  @PostMapping("/cart/update")
  @ApiOperation(value = "修改购物车数量" , notes = "渠道经销商购物车列表页面直接修改")
  public AjaxResult update(@RequestBody ChannelCartParam param) {
    Long channelId = SecurityUtils.getLoginUser().getChannelId();
    boolean rtn = channelCartService.update(new LambdaUpdateWrapper<ChannelCart>()
            .set(ChannelCart::getOrderNum, param.getOrderNum())
        .eq(ChannelCart::getChannelId, channelId)
        .eq(ChannelCart::getCartId, param.getCartId()));
    if (!rtn) {
      return AjaxResult.error("更新失败");
    }
    return AjaxResult.success("更新成功");
  }

  /**
   * 删除购物清单项
   */
  @PostMapping("/cart/remove")
  @ApiOperation(value = "删除购物清单" , notes = "购物车里点删除")
  public AjaxResult remove(@RequestBody BatchLongIdsParam param) {
    boolean rtn = channelCartService.removeByIds(param.getIds());
    if (!rtn) {
      return AjaxResult.error("操作失败");
    }
    return AjaxResult.success("操作成功");
  }

}
