package cn.iocoder.yudao.module.shop.controller;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.core.page.PageDomain;
import cn.iocoder.yudao.module.core.page.TableSupport;
import cn.iocoder.yudao.module.shop.entity.ShopCoupon;
import cn.iocoder.yudao.module.shop.service.IShopCouponService;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.util.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.github.pagehelper.PageHelper;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@RestController
@RequestMapping("/admin-api/system/ShopCoupon")
public class ShopCouponController extends BaseController
{
    @Autowired
    private IShopCouponService shopCouponService;




    /**
     * 查询优惠券列表
     */
//    @PreAuthorize("@ss.hasPermi('system:ShopCoupon:list')")
    @GetMapping("/list")
    public CommonResult list(ShopCoupon shopCoupon,
                             @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                             @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<ShopCoupon> page = new Page<>(pageNum, pageSize);
        IPage<ShopCoupon> list = shopCouponService.selectShopCouponList(page,shopCoupon);
        return success(list);
    }

    /**
     * 导出优惠券列表
     */
//    @PreAuthorize("@ss.hasPermi('system:ShopCoupon:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopCoupon shopCoupon,
                       @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                       @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<ShopCoupon> page = new Page<>(pageNum, pageSize);
        IPage<ShopCoupon> list = shopCouponService.selectShopCouponList(page,shopCoupon);
        ExcelUtil<ShopCoupon> util = new ExcelUtil<ShopCoupon>(ShopCoupon.class);
        util.exportExcel(response, list.getRecords(), "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:ShopCoupon:query')")
    @GetMapping(value = "/{couponId}")
    public CommonResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return success(shopCouponService.selectShopCouponByCouponId(couponId));
    }

    /**
     * 新增优惠券
     */
//    @PreAuthorize("@ss.hasPermi('system:ShopCoupon:add')")
    @PostMapping
    public CommonResult add(@RequestBody ShopCoupon shopCoupon) {
        List<AdminUserDO> selectUserIds = shopCoupon.getSelectUserIds();
        for (AdminUserDO sysUser : selectUserIds) {
            String phoneNumber = sysUser.getMobile();
            if (phoneNumber != null && !phoneNumber.isEmpty()) {
                shopCoupon.setPhonenumber(phoneNumber);
            } else {
                shopCoupon.setPhonenumber("");
            }
            shopCoupon.setUserId(sysUser.getId());
            shopCouponService.insertShopCoupon(shopCoupon);
        }
        return CommonResult.ok();
    }

    /**
     * 修改优惠券
     */
//    @PreAuthorize("@ss.hasPermi('system:ShopCoupon:edit')")
    @PutMapping
    public CommonResult edit(@RequestBody ShopCoupon shopCoupon)
    {
        return toAjax(shopCouponService.updateShopCoupon(shopCoupon));
    }

    /**
     * 删除优惠券
     */
//    @PreAuthorize("@ss.hasPermi('system:ShopCoupon:remove')")
	@DeleteMapping("/{couponIds}")
    public CommonResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(shopCouponService.deleteShopCouponByCouponIds(couponIds));
    }
    /* 我的优惠券 by userId */
    @GetMapping(value = "/listByUserId")
    public CommonResult listByUserId(ShopCoupon shopCoupon,
                                     @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                     @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        shopCoupon.setUserId(getUserId());
        PageDomain pageDomain = TableSupport.getPageDomain();
        Page<ShopCoupon> page = new Page<>(pageNum, pageSize);
        IPage<ShopCoupon> list = shopCouponService.selectShopCouponByUserId(page,shopCoupon);
        return success(list);
    }

    /* 可用优惠券 by userId */
    @GetMapping(value = "/availableCouponsListByUserId")
    public CommonResult availableCouponsListByUserId(ShopCoupon shopCoupon,
                                                     @RequestParam("type") String type,
                                                     @RequestParam(value = "goodId",required = false) Long goodId,
                                                     @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        shopCoupon.setUserId(getUserId());
        if ("jc".equals(type)){
            shopCoupon.setBindMoudle("检测项目现金券");
        }
        if ("qx".equals(type)){
            Integer findNum = shopCouponService.getOffsetCouponCount(getUserId(),goodId);
            if (findNum <= 0 ){
                shopCoupon.setBindMoudle("维修项目现金券");
            }else {
                List<ShopCoupon>  list = shopCouponService.getQxCouponList(getUserId(),goodId);
                return success(list);
            }
        }
        shopCoupon.setCouponStatus("0");
        PageDomain pageDomain = TableSupport.getPageDomain();
        Page<ShopCoupon> page = new Page<>(pageNum, pageSize);
        IPage<ShopCoupon>  list = shopCouponService.availableCouponsListByUserId(page,shopCoupon);
        return success(list);
    }

    /* 可用优惠券 by userId */
    @GetMapping(value = "/unAvailableCouponsListByUserId")
    public CommonResult unAvailableCouponsListByUserId(ShopCoupon shopCoupon,
                                                       @RequestParam("type") String type,
                                                       @RequestParam(value = "goodId",required = false) Long goodId,
                                                       @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        shopCoupon.setUserId(getUserId());
        if ("jc".equals(type)){
            shopCoupon.setBindMoudle("检测项目现金券");
        }
        if ("qx".equals(type)){
            Integer findNum = shopCouponService.getOffsetCouponCount(getUserId(),goodId);
            if (findNum <= 0 ){
                shopCoupon.setBindMoudle("维修项目现金券");
            }else {
                List<ShopCoupon>  list = shopCouponService.unGetQxCouponList(getUserId(),goodId);
                return success(list);
            }
        }
        shopCoupon.setCouponStatus("0");

        PageDomain pageDomain = TableSupport.getPageDomain();
        Page<ShopCoupon> page = new Page<>(pageNum, pageSize);

        IPage<ShopCoupon> list = shopCouponService.unAvailableCouponsListByUserId(page,shopCoupon);
        return success(list);
    }


}
