package com.bweb.controller;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.*;
import com.hfkj.config.CommonSysConst;

import com.hfkj.entity.CouponDiscountGoodsRel;
import com.hfkj.entity.GoodsMsg;
import com.hfkj.entity.GoodsSpecs;
import com.hfkj.model.ResponseData;

import com.hfkj.service.discount.CouponDiscountService;
import com.hfkj.service.goods.BsOrderGoodsService;
import com.hfkj.service.goods.GoodsMsgService;
import com.hfkj.service.goods.GoodsSpecsService;
import com.hfkj.service.goods.GoodsUserAddressService;
import com.hfkj.service.hlt.HltCanteenService;
import com.hfkj.service.hlt.HuiLianTongUnionCardService;
import com.hfkj.service.order.BsOrderChildService;
import com.hfkj.service.pay.huift.TradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Controller
@RequestMapping(value="/test")
@Api(value="测试")
public class TestController {
    private static Logger log = LoggerFactory.getLogger(TestController.class);

    @Resource
    private GoodsMsgService goodsMsgService;

    @Resource
    private GoodsSpecsService goodsSpecsService;

    @Resource
    private CouponDiscountService couponDiscountService;

    @Resource
    private BsOrderChildService orderChildService;
    @Resource
    private BsOrderGoodsService orderGoodsService;
    @Resource
    private BsOrderGoodsService bsOrderGoodsService;

    @Resource
    private GoodsUserAddressService addressService;
    @Resource
    private TradeService huiftTradeService;

    @RequestMapping(value="/thirdPayResult",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询V家园订单交易状态")
    public ResponseData thirdPayResult(@RequestParam(value = "preOrderNo" , required = true) String preOrderNo,
                                       @RequestParam(value = "shopCode" , required = true) String shopCode,
                                       @RequestParam(value = "signSecret" , required = true) String signSecret) {
        try {

            return ResponseMsgUtil.success(huiftTradeService.thirdPayResult(preOrderNo,shopCode,signSecret));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/canteenConsume",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "食堂消费")
    public ResponseData canteenConsume(@RequestParam(value = "cardNo" , required = false) String cardNo,
                                      @RequestParam(value = "instCode" , required = false) String instCode,
                                      @RequestParam(value = "canteenId" , required = false) String canteenId) {
        try {

            String orderNo = OrderUtil.generateOrderNo();

            JSONObject cardInfo = HltCanteenService.canteenConsume(orderNo , cardNo , instCode , canteenId);
            if (StringUtils.isBlank(cardInfo.getString("data"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败");
            }
            return ResponseMsgUtil.success(HuiLianTongUnionCardService.resolveResponse(cardInfo.getString("data")));

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/bindDiscountSpecs",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据类型绑定")
    public ResponseData bindDiscountSpecs(@RequestParam(value = "goodsType" , required = false) Integer goodsType ,
                                          @RequestParam(value = "discountId" , required = false) Long discountId) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("goodsType" , goodsType);
            List<GoodsMsg> goodsMsg = goodsMsgService.getList(map);
            List<GoodsSpecs> specsList = goodsSpecsService.getList(map);

            List<CouponDiscountGoodsRel> goodsRelList = new ArrayList<>();
            for (GoodsMsg goods : goodsMsg) {
                List<GoodsSpecs> list = specsList.stream().filter(specs -> specs.getGoodsId().equals(goods.getId())).
                        collect(Collectors.toList());
                for (GoodsSpecs goodsSpecs : list) {
                    CouponDiscountGoodsRel goodsRel = new CouponDiscountGoodsRel();
                    goodsRel.setDiscountId(discountId);
                    goodsRel.setSpecsId(goodsSpecs.getId());
                    goodsRel.setCreateTime(new Date());
                    goodsRel.setStatus(1);
                    goodsRelList.add(goodsRel);

                }
            }
            couponDiscountService.insertGoodsRelBatch(goodsRelList);
            return ResponseMsgUtil.success("成功");

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/haiOilLogin",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "嗨加油登陆参数")
    public ResponseData haiOilLogin(@RequestBody JSONObject body) {
        try {

            if (body == null|| StringUtils.isBlank(body.getString("phone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            Map<String , Object> map = new HashMap<>();
            map.put("appid" , CommonSysConst.getSysConfig().getHaiOilAppid());
            map.put("phone" , body.getString("phone"));
            map.put("sign" , SignatureUtil.createSign(map , CommonSysConst.getSysConfig().getHaiOilAppSecret()));


            return ResponseMsgUtil.success(map);

        } catch (Exception e) {
            log.error("error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
