package com.fs.his.controller;

import java.util.List;

import com.fs.common.exception.CustomException;
import com.fs.his.param.FsCouponParam;
import com.fs.his.vo.FsCouponAllListVO;
import com.fs.his.vo.FsCouponListVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsCoupon;
import com.fs.his.service.IFsCouponService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author fs
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/his/coupon")
public class FsCouponController extends BaseController
{
    @Autowired
    private IFsCouponService fsCouponService;

    /**
     * 查询优惠券列表订单查询
     */
    @PreAuthorize("@ss.hasPermi('his:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCouponParam fsCoupon)
    {
        startPage();
        List<FsCouponListVO> list = fsCouponService.selectFsCouponListVO(fsCoupon);
        return getDataTable(list);
    }

    /**
     * 查询优惠券列表订单查询
     */
    @GetMapping("/allList")
    public AjaxResult allList()
    {
        List<FsCouponAllListVO> list = fsCouponService.selectFsCouponListAll();
        return AjaxResult.success(list);
    }



    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('his:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCoupon fsCoupon)
    {
        List<FsCoupon> list = fsCouponService.selectFsCouponList(fsCoupon);
        ExcelUtil<FsCoupon> util = new ExcelUtil<FsCoupon>(FsCoupon.class);
        return util.exportExcel(list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:coupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return AjaxResult.success(fsCouponService.selectFsCouponByCouponId(couponId));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('his:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCoupon fsCoupon)
    {
        return toAjax(fsCouponService.insertFsCoupon(fsCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('his:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCoupon fsCoupon)
    {
        FsCoupon c = fsCouponService.selectFsCouponByCouponId(fsCoupon.getCouponId());
        long l = c.getRemainNumber() + fsCoupon.getNumber() - c.getNumber();
        if (l<0){
            throw new CustomException("剩余卷不足");
        }
        fsCoupon.setRemainNumber(l);
        return toAjax(fsCouponService.updateFsCoupon(fsCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('his:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(fsCouponService.deleteFsCouponByCouponIds(couponIds));
    }
}
