package com.pj.tb_goods_transit;


import com.pj.api.dto.GoodsTransitDto;

import com.pj.common.core.exception.ServiceException;
import com.pj.enummj.DeleteStatus;
import com.pj.enummj.GoodsStatus;
import com.pj.enummj.IsOrders;
import com.pj.tb_goods_transit.param.PurchaseLevelOneGoodsTransitParam;
import com.pj.tb_goods_transit.param.TransactionGoodsParam;
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.*;

import java.util.List;

/**
 * app端用户添加/上架/下架商品
 *
 * @Author Mechrevo
 * @Date 2023 07 27 09 31
 **/
@RestController
@RequestMapping("/app/TbGoodsTransit/")
public class GoodsTransitAppController {

    @Autowired
    private TbGoodsTransitService tbGoodsTransitService;


    /**
     * 查 - 根据id
     */
    @RequestMapping("getById")
//		@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE)
    public AjaxJson getById(Long id) {
        TbGoodsTransit t = tbGoodsTransitService.getById(id);
        return AjaxJson.getSuccessData(t);
    }

    /**
     * 查集合 - 根据自己的互市区ID查询对应互市区的商品
     */
    @RequestMapping("getTransitList")
    public AjaxJson getTransitList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbGoodsTransit> list = tbGoodsTransitService.getAppList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 一级市场
     */
    @RequestMapping("getList")
    public AjaxJson getList() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("goodsStatus", GoodsStatus.GOODS_STATUS_ON.getCode());
        so.put("isOrders", IsOrders.IS_ORDERS_ZERO.getCode());
        so.put("deleteStatus", DeleteStatus.DELETE_STATUS_ON.getCode());

        List<TbGoodsTransit> list = tbGoodsTransitService.getList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 删
     */
    @RequestMapping("deleteById")
//	@SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_DEL)
    public AjaxJson deleteById(Long id) {
        tbGoodsTransitService.delete(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 改
     */
    @RequestMapping("update")
    public AjaxJson update(TbGoodsTransit t) {
        //修改前判断该商品是否已被下单,下单则无法进行下架
        if(t.getIsOrders().equals(IsOrders.IS_ORDERS_ONE.getCode())){
            throw new ServiceException("您的商品[ " + t.getGoodsName() + " ]已被下单,暂时无法修改信息,请先往处理订单再进行修改。");
        }
        tbGoodsTransitService.update(t);
        return AjaxJson.getSuccess();
    }


    /**
     * app端用户添加商品
     */
    @RequestMapping("add")
    public AjaxJson add(TransactionGoodsParam t) {
        return AjaxJson.toAjax(tbGoodsTransitService.transactionGoods(t));
    }

    /**
     * app端用户上架/下架商品
     */
    @RequestMapping("UpOrDownGoods")
//    @SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_ADD)
    public AjaxJson UpOrDownGoods(@RequestParam("goodsTransitId") Long goodsTransitId, @RequestParam("goodsStatus") Integer goodsStatus) {
        boolean goods = tbGoodsTransitService.UpOrDownGoods(goodsTransitId, goodsStatus);
        if (goods) {
            if (!goodsStatus.toString().trim().equals("")) {
                if (goodsStatus == 1) {
                    return AjaxJson.getSuccess("商品上架成功!");
                }
                return AjaxJson.getSuccess("商品下架成功!");
            }
        }
        return AjaxJson.getError("商品操作失败!");
    }


    /**
     * 采购一级市场的商品，
     * 包括加入购物车、购买、支付（暂时不做），
     * 下单后会向组内边民发送短信，
     * 通知其登录APP进行交易确认。
     */
    @RequestMapping("purchaseLevelOntGoodsTransit")
//    @SaCheckPermission(TbGoodsTransit.PERMISSION_CODE_ADD)
    public AjaxJson purchaseLevelOntGoodsTransit(@Validated @RequestBody PurchaseLevelOneGoodsTransitParam purchaseLevelOneGoodsTransitParam) throws Exception {
        boolean goods = tbGoodsTransitService.purchaseLevelOntGoodsTransit(purchaseLevelOneGoodsTransitParam);
        if (goods) {
            return AjaxJson.getSuccess("提交成功,待商家确认订单!");
        }
        return AjaxJson.getError("提交失败!");
    }

    /*-----       远程调用👇👇  正常业务方法👆👆    -----*/

    /**
     * 根据商户ID和过审商品表ID查询过审商品
     */
    @RequestMapping("rpc/getGoodsTransitById")
    public GoodsTransitDto getGoodsTransitById(@RequestParam("goodsTransitId") Long goodsTransitId,@RequestParam("enterpriseId") Long enterpriseId) {
        GoodsTransitDto t = tbGoodsTransitService.getGoodsTransitById(goodsTransitId,enterpriseId);
        return t;
    }

}
