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 shop.alien.entity.result.R;
import shop.alien.entity.store.LifePromotionPackage;
import shop.alien.entity.store.vo.LifePromotionPackageVo;
import shop.alien.store.service.PromotionPackageService;
import shop.alien.util.common.AlipayTradeAppPay;
import shop.alien.util.common.UniqueRandomNumGenerator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = {"推广套餐"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/promotionPackage")
@RequiredArgsConstructor
public class LifePromotionPackageController {

    private final PromotionPackageService promotionPackageService;

    @ApiOperation("获取推广套餐")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "expert_id", value = "达人id", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "大小", dataType = "Integer", paramType = "query", required = true)}
    )
    @GetMapping("/getByExpertId")
    public R<IPage<LifePromotionPackageVo>> getByExpertId(@RequestParam("expertId") Long expertId,@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifePromotionPackageController.getByExpertId?id={},page={},size={}", expertId,page,size);
        return R.data(promotionPackageService.getByExpertId(expertId, page, size));
    }

    @ApiOperation("新增或修改推广套餐")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveOrUpdate")
    public R<Boolean> saveOrUpdate(@RequestBody LifePromotionPackage lifePromotionPackage) {
        log.info("LifePromotionPackageController.saveOrUpdate?lifePromotionPackageInfo={}", lifePromotionPackage);
        if (promotionPackageService.saveOrUpdate(lifePromotionPackage)) {
            if (null != lifePromotionPackage.getId()) {
                return R.success("修改成功");
            }
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation(value = "删除推广套餐")
    @ApiOperationSupport(order = 3)
    @PostMapping("/delete")
    public R<String> delete(@RequestBody List<Integer> ids) {
        log.info("LifePromotionPackageController.delete?ids={}", ids);
        if (promotionPackageService.removeByIds(ids)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    @ApiOperation(value = "商户购买推广套餐")
    @GetMapping("/promotionPackagePay")
    @ApiOperationSupport(order = 4)
    public R<Map<String, Object>> promotionPackagePay(@RequestParam("price") String price) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderNo = UniqueRandomNumGenerator.generateUniqueCode(19);
        String executeAlipayPayment = AlipayTradeAppPay.executeAlipayPayment(dateFormat.format(new Date()), price, "推广套餐支付");
        if (!executeAlipayPayment.equals("调用失败") && !executeAlipayPayment.isEmpty()) {
            Map<String, Object> returnMap = new HashedMap<>();
            returnMap.put("orderStr", executeAlipayPayment);
            returnMap.put("orderNo", orderNo);
            return R.data(returnMap);
        } else {
            return R.fail("支付失败");
        }
    }


    @ApiOperation("支付推广套餐成功后调用")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "promotionPackageId", value = "promotionPackageId", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "expertId", value = "expertId", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "payPrice", value = "payPrice", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "orderNo", dataType = "String", paramType = "query")})
    @PostMapping("/buyPromotion")
    public R<Map<String, Object>> buyPromotion(@RequestBody LifePromotionPackageVo lifePromotionPackageVo) {
        log.info("LifeUserStoreController.buyPromotion?promotionPackageId={},userId={},payPrice={},orderNo={}", lifePromotionPackageVo);
        return R.data(promotionPackageService.buyPromotion(lifePromotionPackageVo));
    }


}
