package com.pj.tb_orders;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pj.api.consts.FeignFactory;
import com.pj.api.dto.OrderDto;
import com.pj.current.dto.APPLoginUserInfo;
import com.pj.current.satoken.StpAPPUserUtil;
import com.pj.project4sp.SP;
import com.pj.tb_goods_demand.vo.GoodsDemandVo;
import com.pj.tb_orders.vo.OrderStaticsDTO;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * Controller: tb_orders -- 订单表
 *
 * @author qzy
 */
@RestController
@RequestMapping("/app/TbOrders/")
public class TbOrdersApiController {

    /**
     * 底层 Service 对象
     */
    @Autowired
    TbOrdersService tbOrdersService;


    /**
     * 增
     */
    @RequestMapping("add")
    public AjaxJson add(GoodsDemandVo t) {
        return AjaxJson.toAjax(tbOrdersService.addByDetail(t));
    }

    /**
     * 删
     */
    @RequestMapping("delete")
    public AjaxJson delete(Long id) {
        tbOrdersService.delete(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 删 - 根据id列表
     */
    @RequestMapping("deleteByIds")
    public AjaxJson deleteByIds() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        int line = SP.publicMapper.deleteByIds(TbOrders.TABLE_NAME, ids);
        return AjaxJson.getByLine(line);
    }

    /**
     * 改 收购商购买
     */
    @RequestMapping("purchaserBuy")
    public AjaxJson purchaserBuy() {
        SoMap so = SoMap.getRequestSoMap();
        String orderIds = so.getString("ids");
        // 获取app登录用户
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        return AjaxJson.toAjax(tbOrdersService.purchaserBuy(orderIds, appLoginInfo.getFk()));
    }

    @RequestMapping("purchaserBuyOneLevelOrder")
    public AjaxJson purchaserBuyOneLevelOrder() {
        List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class);
        List<TbOrders> orders = tbOrdersService.findByLevelOneOrderIds(ids);
        String orderIds = orders.stream().map(obj -> obj.getId() + "").collect(Collectors.joining(","));
        // 获取app登录用户
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        return AjaxJson.toAjax(tbOrdersService.purchaserBuy(orderIds, appLoginInfo.getFk()));
    }

    /**
     * 采购商整车购买
     *
     * @return
     */
    @RequestMapping("purchaserBuyWhole")
    public AjaxJson purchaserBuyWhole() {
        SoMap so = SoMap.getRequestSoMap();
        String ids = so.getString("ids");
        List<OrderDto> orderDtos = FeignFactory.levelOneServerInterface.getOrderListByMainIds(ids);
        List<Long> leveloneOrderIds = orderDtos.stream().map(OrderDto::getId).collect(Collectors.toList());
        List<TbOrders> orders = tbOrdersService.findByLevelOneOrderIds(leveloneOrderIds);
        if (orders.isEmpty()) {
            return AjaxJson.getError("订单不存在");
        }
        String orderIds = orders.stream().map(obj -> obj.getId() + "").collect(Collectors.joining(","));
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        return AjaxJson.toAjax(tbOrdersService.purchaserBuy(orderIds, appLoginInfo.getFk()));
    }

    /** 新增 边民缴税以及各服务费*/
	/*@RequestMapping("payTax")
	public AjaxJson payTax(){
		SoMap so = SoMap.getRequestSoMap();
		return AjaxJson.toAjax(tbOrdersService.payTax(so));
	}*/

    /**
     * 查集合 - 查二级市场订单(未被购买的订单)
     */
    @RequestMapping("getLevelTwoList")
    public AjaxJson getLevelTwoList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrders> list = tbOrdersService.getLevelTwoList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 获取在售订单
     *
     * @return
     */
    @RequestMapping("getOnSaleList")
    public AjaxJson getOnSaleList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrders> list = tbOrdersService.getOnSaleList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 二级市场订单 组长/边民角色入口
     */
    @RequestMapping("getLeaderOrderList")
    public AjaxJson getLeaderOrderList() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrders> list = tbOrdersService.getLeaderOrderList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    /**
     * 二级市场订单 收购商角色入口
     */
    @RequestMapping("getPurchaserOrderList")
    public AjaxJson getPurchaserOrderList() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("purchaserId", StpAPPUserUtil.getAPPLoginInfo().getFk());
        List<TbOrders> list = tbOrdersService.getPurchaserOrderList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @RequestMapping("getPurchaserStatistics")
    public AjaxJson getPurchaserStatistics() {
        SoMap so = SoMap.getRequestSoMap();
        APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
        so.put("purchaserId", appLoginInfo.getFk());
        List<OrderStaticsDTO> list = tbOrdersService.getPurchaserStatistics(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }


    /**
     * 改 - 收购商确认收货
     */
    @RequestMapping("toDelivery")
    public AjaxJson toDelivery(TbOrders t) {
        return AjaxJson.toAjax(tbOrdersService.updateById(t));
    }

    /**
     * 改 - 删除状态(0=禁用,1=启用)
     */
    @RequestMapping("updateDeleteStatus")
//	@SaCheckPermission(TbOrders.PERMISSION_CODE_EDIT)
    public AjaxJson updateDeleteStatus(Long id, Integer value) {
        int line = SP.publicMapper.updateColumnById(TbOrders.TABLE_NAME, "delete_status", value, id);
        return AjaxJson.getByLine(line);
    }


    /**
     * 增 一级市场已完成订单进行转售时，新增二级市场订单
     */
    @RequestMapping("addOrderByResale")
    public AjaxJson addOrderByResale() {
        if (!StpAPPUserUtil.isLogin()) {
            return AjaxJson.getError("请先登录");
        }
        SoMap so = SoMap.getRequestSoMap();
        return AjaxJson.toAjax(tbOrdersService.addOrderByResale(so));
    }


    /**
     * 计算 订单转售时,计算当前订单费项总和
     */
    @RequestMapping("countPrice")
    public AjaxJson countPrice() {
        SoMap so = SoMap.getRequestSoMap();
        return AjaxJson.getSuccessData(tbOrdersService.countPrice(so));
    }

    /**
     * 查 根据订单Id查订单详情
     */
    @RequestMapping("getDetailById")
    public AjaxJson getDetailById() {
        SoMap so = SoMap.getRequestSoMap();
        List<TbOrders> list = tbOrdersService.getDetailById(so);
        return AjaxJson.getSuccessData(list);
    }

    /**
     * 根据一级市场id查询二级市场订单
     */
    @RequestMapping("getByLevelOneOrderId")
    public AjaxJson getByLevelOneOrderId() {
        SoMap so = SoMap.getRequestSoMap();
        return AjaxJson.getSuccessData(tbOrdersService.getByLevelOneOrderId(so));
    }

    /**
     * 修改转售金额
     */
    @RequestMapping("updateResalePrice")
    public AjaxJson updateResalePrice(TbOrders tbOrders) {
        return AjaxJson.toAjax(tbOrdersService.updateResalePrice(tbOrders));
    }


    @RequestMapping("deduction")
    public AjaxJson deduction(Long id) {
        if (!StpAPPUserUtil.isLogin()) {
            return AjaxJson.getError("用户未登录");
        }
        TbOrders tbOrders = tbOrdersService.getById(id);
        if (tbOrders == null) {
            return AjaxJson.getError("订单不存在");
        }
        if (tbOrders.getIsPay() != 2) {
            return AjaxJson.getError("订单无法执行补扣");
        }
        Long fkId = StpAPPUserUtil.getAPPLoginInfo().getFk();
        if (!Objects.equals(fkId, tbOrders.getPurchaserId())) {
            return AjaxJson.getError("您无权执行此操作");
        }
        tbOrdersService.deduction(id);
        return AjaxJson.getSuccess();
    }

    /**
     * 查 - 根据一级市场订单id查二级市场的转售金额
     */
    @RequestMapping("rpc/getResalePriceByLevelOneOrderId")
    public Double getResalePriceByLevelOneOrderId(@RequestParam("orderId") Long orderId) {
        SoMap so = new SoMap();
        so.put("levelOneOrderId", orderId);
        TbOrders orders = tbOrdersService.getByLevelOneOrderId(so);
        Double resalePrice = orders.getResalePrice();
        return resalePrice;
    }


    /**
     * 采购商支付成功 ——> 边民缴税以及各服务费
     */
    @PostMapping("rpc/payTax")
    public boolean payTax(@RequestParam("orderNo") String orderNo, @RequestParam("MKT008Status") Integer MKT008Status) {
        boolean result = tbOrdersService.payTax(orderNo, MKT008Status);
        return result;
    }

    /**
     * 收到边民缴税以及各服务费回执
     */
    @PostMapping("rpc/payTaxFromBank")
    public boolean payTaxFromBank(@RequestParam("orderNo") String orderNo, @RequestParam("MKT008Status") Integer MKT008Status) {
        boolean result = tbOrdersService.payTaxFromBank(orderNo, MKT008Status);
        return result;
    }

}
