package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import shop.alien.entity.result.R;
import shop.alien.mapper.StoreCommentAppealMapper;
import shop.alien.mapper.StoreCommentMapper;
import shop.alien.store.service.LifeUserStoreService;
import shop.alien.util.common.AlipayTradeAppPay;
import shop.alien.util.common.ListToPage;
import shop.alien.util.common.UniqueRandomNumGenerator;

import java.util.List;
import java.util.Map;

@Api(tags = {"一期-用户店铺"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/userstore")
@RequiredArgsConstructor
public class UserStoreController {

    private final LifeUserStoreService lifeUserStoreService;

    private final RestTemplate restTemplate;

    private final StoreCommentAppealMapper storeCommentAppealMapper;

    private final StoreCommentMapper storeCommentMapper;

//    @Value("${third-party-login.base-url}")
    private String loginUrl = "http://192.168.2.250:9000/ai/user-auth-core/api/v1/auth/login";

    private String resultUrl = "http://192.168.2.250:9000/ai/auto-review/api/v1/records/{record_id}/completed";

//    @Value("${third-party-user-name-admin.base-url}")
    private String userName = "UdUser";

//    @Value("${third-party-pass-word.base-url}")
    private String passWord = "123456";

    /**
     * 差评申述置信度分析接口地址
     * 例如: http://192.168.2.250:9004/api/v1/analyze 或通过网关: http://192.168.2.250:9000/ai/auto_review/api/v1/analyze
     */
//    @Value("${bad-review-analyze.base-url}")
    private String analyzeUrl = "http://192.168.2.250:9000/ai/auto-review/api/v1/analyze";

    @ApiOperation("查询商铺列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeType", value = "storeType", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeName", value = "storeName", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "distance", value = "distance", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "jingdu", value = "jingdu", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "weidu", value = "weidu", dataType = "String", paramType = "query")})
    @GetMapping("/getStoreList")
    public R<IPage<Map<String, Object>>> getStoreList(@RequestParam("storeType") String storeType,
                                                      @RequestParam("storeName") String storeName,
                                                      @RequestParam("distance") Integer distance,
                                                      @RequestParam("jingdu") String jingdu,
                                                      @RequestParam("weidu") String weidu,
                                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                      @RequestParam(value = "size", defaultValue = "10") Integer size) {
        log.info("LifeUserStoreController.getStoreList?page={},size={},storeType={},storeName={},distance={},jingdu={},weidu={}", page, size, storeType, storeName, distance, jingdu, weidu);
        List<Map<String, Object>> result = lifeUserStoreService.getStoreList(storeType, storeName, distance, jingdu, weidu, page, size);
        return R.data(ListToPage.setPage(result, page, size));
    }

    @ApiOperation("查询商铺详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "商铺id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jingdu", value = "jingdu", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "weidu", value = "weidu", dataType = "String", paramType = "query")})
    @GetMapping("/getStoreDetailById")
    public R<Map<String, Object>> getStoreDetailById(@RequestParam("storeId") String storeId,
                                                     @RequestParam("userId") String userId,
                                                     @RequestParam("jingdu") String jingdu,
                                                     @RequestParam("weidu") String weidu) {
        log.info("LifeUserStoreController.getStoreDetailById?storeId={},userId={},jingdu={},weidu={}", storeId, userId, jingdu, weidu);
        return R.data(lifeUserStoreService.getStoreDetailById(storeId, userId, jingdu, weidu));
    }

    @ApiOperation("提交支付团购套餐和代金券前检查")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "quanId", value = "quanId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cnt", value = "cnt", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "payPrice", value = "payPrice", dataType = "String", paramType = "query")})
    @GetMapping("/buyQuanCheck")
    @Deprecated
    public R<Map<String, Object>> buyQuanCheck(@RequestParam("quanId") String quanId,
                                               @RequestParam("userId") String userId,
                                               @RequestParam("cnt") int cnt,
                                               @RequestParam("payPrice") String payPrice) {
        log.info("LifeUserStoreController.buyQuanCheck?quanId={},userId={},cnt={},payPrice={}", quanId, userId, cnt, payPrice);
        return R.data(lifeUserStoreService.buyQuanCheck(quanId, userId, cnt, payPrice));
    }

    @ApiOperation("获取支付信息")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "payPrice", value = "payPrice", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "quanName", value = "quanName", dataType = "String", paramType = "query")})
    @GetMapping("/getOrderStrForPay")
    @Deprecated
    public R<Map<String, Object>> getOrderStrForPay(@RequestParam("payPrice") String payPrice,
                                                    @RequestParam("quanName") String quanName) {
        log.info("LifeUserStoreController.getOrderStrForPay?payPrice={},quanName={}", payPrice, quanName);
        String orderNo = UniqueRandomNumGenerator.generateUniqueCode(19);
        String orderStr = AlipayTradeAppPay.executeAlipayPayment(orderNo, payPrice, quanName);
        if (!orderStr.equals("调用失败") && !orderStr.isEmpty()) {
            Map<String, Object> returnMap = new HashedMap<>();
            returnMap.put("orderStr", orderStr);
            returnMap.put("orderNo", orderNo);
            return R.data(returnMap);
        } else {
            return R.fail("支付失败");
        }
    }

    @ApiOperation("支付团购套餐和代金券成功后调用")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "quanId", value = "quanId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cnt", value = "购买数量", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "payPrice", value = "payPrice", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "orderNo", dataType = "String", paramType = "query")})
    @GetMapping("/buyQuan")
    @Deprecated
    public R<Map<String, Object>> buyQuan(@RequestParam("quanId") String quanId,
                                          @RequestParam("couponId") String couponId,
                                          @RequestParam("userId") String userId,
                                          @RequestParam("cnt") int cnt,
                                          @RequestParam("payPrice") String payPrice,
                                          @RequestParam("orderNo") String orderNo) {
        log.info("LifeUserStoreController.buyQuan?quanId={},couponId={},userId={},cnt={},payPrice={},orderNo={}", quanId, couponId, userId, cnt, payPrice, orderNo);
        return R.data(lifeUserStoreService.buyQuan(quanId, couponId, userId, cnt, payPrice, orderNo));
    }

}
