package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.EssentialHolidayComparison;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.vo.LifeCouponStatusVo;
import shop.alien.entity.store.vo.LifeCouponVo;
import shop.alien.store.annotation.TrackEvent;
import shop.alien.store.service.LifeCouponService;

import java.util.Map;

/**
 * 优惠券
 */
@Api(tags = {"一期-优惠券"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/coupon")
@RequiredArgsConstructor
public class LifeCouponController {

    private final LifeCouponService lifeCouponService;

    @ApiOperation("新建代金券")
    @PostMapping("/addOrUpdateCoupon")
    public R<LifeCoupon> addOrUpdateCoupon(@RequestBody LifeCoupon lifeCoupon) {
        log.info("LifeCouponController.addOrUpdateCoupon?lifeCoupon={}", lifeCoupon.toString());
        return R.data(lifeCouponService.addOrUpdateCoupon(lifeCoupon));
    }

    /**
     * 修改库存数量
     *
     * @param id        主键
     * @param singleQty 库存数量
     * @return boolean
     */
    @ApiOperation("修改库存数量")
    @GetMapping("/updateCouponSingleQty")
    public R<Boolean> updateCouponSingleQty(@RequestParam("id") Integer id, @RequestParam("singleQty") Integer singleQty) {
        log.info("LifeCouponController.updateCouponSingleQty?id={}&singleQty={}", id, singleQty);
        if (lifeCouponService.updateCouponSingleQty(id, singleQty)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("代金劵列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query", required = false), @ApiImplicitParam(name = "name", value = "名称", dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "dataType", value = "数据类型: 0:正式数据, 1:草稿数据", dataType = "Integer", paramType = "query", defaultValue = "0")})
    @GetMapping("/getCouponList")
    public R<IPage<LifeCoupon>> getCouponList(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size, @RequestParam("storeId") String storeId, @RequestParam("status") String status, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "dataType", defaultValue = "0") Integer dataType) {
        log.info("LifeCouponController.getCouponList?page={},size={},storeId={},status={},name={},dataType={}", page, size, storeId, status, name, dataType);
        return R.data(lifeCouponService.getCouponList(page, size, storeId, status, name, dataType));
    }

    @ApiOperation("暂停恢复优惠券")
    @GetMapping("/updateCoupon")
    public R<Boolean> updateCoupon(@RequestParam("id") String id, @RequestParam("status") Integer status) {
        log.info("LifeCouponController.updateCoupon?id={}&status={}", id, status);
        if (lifeCouponService.updateCoupon(id, status) > 0) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("优惠券详情")
    @GetMapping("/getCouponDetail")
    private R<LifeCoupon> getCouponDetail(@RequestParam("id") String id) {
        log.info("LifeCouponController.getCouponDetail?id={}", id);
        LambdaQueryWrapper<LifeCoupon> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(LifeCoupon::getId, id);
        return R.data(lifeCouponService.getOne(objectLambdaQueryWrapper));
    }

    @ApiOperation("代金劵详情")
    @GetMapping("/getNewCouponDetail")
    public R<LifeCouponVo> getNewCouponDetail(@RequestParam("id") String id) {
        log.info("LifeCouponController.getNewCouponDetail?id={}", id);
        try {
            LifeCouponVo lifeCouponVo = lifeCouponService.getNewCouponDetail(id);
            if (lifeCouponVo == null) {
                return R.fail("代金券不存在或已删除");
            }
            return R.data(lifeCouponVo);
        } catch (Exception e) {
            log.error("LifeCouponController.getNewCouponDetail ERROR Msg={}", e.getMessage(), e);
            return R.fail("查询失败");
        }
    }

//    @TrackEvent(
//            eventType = "COUPON_USE",
//            eventCategory = "COUPON",
//            storeId = "#{#storeId}",
//            targetType = "COUPON"
//    )
    @ApiOperation("旧 核销订单")
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "quanCode", value = "券码", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/verify")
    public R<Map<String, String>> verify(@RequestParam("storeId") String storeId, @RequestParam("quanCode") String quanCode) {
        log.info("LifeCouponController.verify?storeId={},quanCode={}", storeId, quanCode);
        return R.data(lifeCouponService.couponVerify(storeId, quanCode));
    }

    @ApiOperation("新 核销订单")
    @ApiImplicitParams({@ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "quanCode", value = "券码", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/newVerify")
    public R<Map<String, String>> newVerify(@RequestParam("storeId") String storeId, @RequestParam("quanCode") String quanCode) {
        log.info("LifeCouponController.newVerify?storeId={},quanCode={}", storeId, quanCode);
        return R.data(lifeCouponService.newCouponVerify(storeId,quanCode));
    }

    @ApiOperation("核销订单前效验")
    @ApiImplicitParams(@ApiImplicitParam(name = "orderCode", value = "劵code", dataType = "String", paramType = "query", required = true))
    @GetMapping("/orderVerify")
    public R<String> orderVerify(@RequestParam("orderCode") String orderCode) {
        log.info("LifeCouponController.orderVerify?orderCode={}", orderCode);
        return lifeCouponService.orderVerify(orderCode);
    }

    @ApiOperation("获取优惠券状态")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getCouponStatus")
    @ApiImplicitParams({@ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", paramType = "query", required = true),})
    public R<LifeCouponStatusVo> getCouponStatus(@RequestParam(value = "couponId") String couponId) {
        log.info("LifeDiscountCouponController.getCouponStatus?couponId={}", couponId);
        try {
            LifeCouponStatusVo lifeCouponStatusVo = lifeCouponService.getCouponStatus(couponId);
            return R.data(lifeCouponStatusVo);
        } catch (Exception e) {
            log.error("LifeDiscountCouponController.getCouponStatus ERROR Msg={}", e.getMessage());
            return R.fail("查询失败");
        }
    }

    @ApiOperation("节假日列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页数", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "size", value = "页容", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "year", value = "年份", dataType = "String", paramType = "query", required = false), @ApiImplicitParam(name = "openFlag", value = "是否启用(0:未启用,1:已启用)", dataType = "String", paramType = "query", required = false), @ApiImplicitParam(name = "holidayName", value = "节假日名称", dataType = "String", paramType = "query", required = false)})
    @GetMapping("/getHolidayList")
    private R<IPage<EssentialHolidayComparison>> getHolidayList(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "10") int size, @RequestParam("year") String year, @RequestParam("openFlag") String openFlag, @RequestParam("holidayName") String holidayName) {
        log.info("LifeCouponController.getHolidayList?page={},size={},year={},openFlag={},holidayName={}", page, size, year, openFlag, holidayName);
        return R.data(lifeCouponService.getHolidayList(page, size, year, openFlag, holidayName));
    }

    @ApiOperation("保存节假日")
    @PostMapping("/saveHoliday")
    public R<Boolean> saveHoliday(@RequestBody EssentialHolidayComparison essentialHolidayComparison) {
        log.info("LifeCouponController.saveHoliday?essentialHolidayComparison={}", essentialHolidayComparison.toString());
        if (lifeCouponService.saveHoliday(essentialHolidayComparison)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("删除节假日")
    @GetMapping("/delHoliday")
    private R delHoliday(@RequestParam("id") String id) {
        log.info("LifeCouponController.delHoliday?id={}", id);
        if (lifeCouponService.delHoliday(id)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("启用禁用节假日")
    @GetMapping("/openCloseHoliday")
    private R openCloseHoliday(@RequestParam("id") String id) {
        log.info("LifeCouponController.openCloseHoliday?id={}", id);
        if (lifeCouponService.openCloseHoliday(id)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("按年份删除节假日")
    @GetMapping("/delHolidayByYear")
    private R delHolidayByYear(@RequestParam("year") String year) {
        log.info("LifeCouponController.delHolidayByYear?year={}", year);
        if (lifeCouponService.delHolidayByYear(year)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("导入假期管理")
    //@ApiImplicitParams({@ApiImplicitParam(name = "file", value = "Excel文件", dataType = "MultipartFile", paramType = "form", required = true)})
    @PostMapping("/importHoliday")
    public R<String> importHoliday(MultipartFile file) {
        log.info("LifeCouponController.importHoliday fileName={}", file.getOriginalFilename());
        return lifeCouponService.importHolidayFromExcel(file);
    }
    /**
     * 删除代金券
     * 根据优惠券ID删除代金券信息
     *
     * @param id 优惠券ID
     * @return R<Boolean> 操作结果
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("删除代金券")
    @GetMapping("/deleteCoupon")
    public R<Boolean> deleteCoupon(@RequestParam("id") Integer id) {
        log.info("LifeCouponController.updateCouponSingleQty?id={}", id);
        if (lifeCouponService.deleteCoupon(id)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }
}
