package cn.pluss.platform.controller.home;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.deviceStock.DeviceStockService;
import cn.pluss.platform.entity.DeviceStock;
import cn.pluss.platform.entity.OfficialGoods;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.officialGoods.exchange.OfficialGoodsExchangeService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author SSS
 */
@Controller
@RequestMapping("/merchantDevice")
public class MerchantDeviceController {

    @Autowired
    private UserAppService uaService;

    @Autowired
    private OfficialGoodsExchangeService officialGoodsExchangeService;

    @Autowired
    private DeviceStockService dsService;

    /**
     * 流量卡充值开始页面
     *
     * @return
     */
    @GetMapping("/simChargeIndex")
    public String simChargeIndex(HttpServletRequest request, String token) {
        try {
            request.setAttribute("token", token);
            return "device/simChargeIndex";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "拒绝访问！");
            return "merchant/errorInfo2";
        }
    }

    @GetMapping("/simOrder")
    public String simOrder(HttpServletRequest request, String token, String snNo) {
        try {
            LambdaQueryWrapper<DeviceStock> qWrapper = Wrappers.lambdaQuery();
            qWrapper.eq(DeviceStock::getDeviceNo, snNo).or().eq(DeviceStock::getSnNo, snNo);
            DeviceStock one = dsService.getOne(qWrapper.last("limit 1"));
            if (one == null) {
                throw new MsgException("设备编号不存在");
            }

            if (!one.getCode().contains("4G")) {
                throw new MsgException("该设备编码不需要通信服务费");
            }

            if (!"3".equals(one.getStatus())) {
                throw new MsgException("该设备暂未绑定商户");
            }

            request.setAttribute("token", token);
            request.setAttribute("snNo", snNo);
            request.setAttribute("device", one);
            return "device/order";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", e.getMessage());
            return "merchant/errorInfo2";
        }
    }

    @PostMapping("/simOrder")
    @ResponseBody
    public Result<Object> createSimChargeOrder(HttpServletRequest request, OfficialGoods goods, String token) {
        LambdaQueryWrapper<DeviceStock> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(DeviceStock::getDeviceNo, goods.getAddress()).or().eq(DeviceStock::getSnNo, goods.getAddress());
        DeviceStock one = dsService.getOne(qWrapper.last("limit 1"));
        if (one == null) {
            throw new MsgException("设备编号不存在");
        }

        if (!one.getCode().contains("4G")) {
            throw new MsgException("该设备编码不需要通信服务费");
        }

        if (!"3".equals(one.getStatus())) {
            throw new MsgException("该设备暂未绑定商户");
        }

        UserApp own = new UserApp();
        own.setUserId(1791L);
        own = uaService.getOne(new QueryWrapper<>(own));

        goods.setId(10086);
        goods.setIsPostage(1);
        goods.setPostageFee(BigDecimal.ZERO);
        goods.setDeductValue(BigDecimal.TEN);
        goods.setGoodPrice(BigDecimal.TEN);
        goods.setDeductRule(1);
        return officialGoodsExchangeService.exchange(goods, own);
    }
}
