package com.fs.store.controller;

import java.util.Date;
import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.store.domain.FsStoreCouponIssue;
import com.fs.store.param.FsStoreCouponPublishParam;
import com.fs.store.service.IFsStoreCouponIssueService;
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.store.domain.FsStoreCoupon;
import com.fs.store.service.IFsStoreCouponService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/storeCoupon")
public class FsStoreCouponController extends BaseController
{
    @Autowired
    private IFsStoreCouponService fsStoreCouponService;
    @Autowired
    private IFsStoreCouponIssueService fsStoreCouponIssueService;
    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreCoupon fsStoreCoupon)
    {
        startPage();
        fsStoreCoupon.setIsDel(0);
        List<FsStoreCoupon> list = fsStoreCouponService.selectFsStoreCouponList(fsStoreCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeCoupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsStoreCoupon fsStoreCoupon)
    {
        List<FsStoreCoupon> list = fsStoreCouponService.selectFsStoreCouponList(fsStoreCoupon);
        ExcelUtil<FsStoreCoupon> util = new ExcelUtil<FsStoreCoupon>(FsStoreCoupon.class);
        return util.exportExcel(list, "storeCoupon");
    }

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

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('store:storeCoupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreCoupon fsStoreCoupon)
    {
        return toAjax(fsStoreCouponService.insertFsStoreCoupon(fsStoreCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('store:storeCoupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreCoupon fsStoreCoupon)
    {
        return toAjax(fsStoreCouponService.updateFsStoreCoupon(fsStoreCoupon));
    }

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

    @PreAuthorize("@ss.hasPermi('store:storeCoupon:publish')")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody FsStoreCouponPublishParam publishParam)
    {
        FsStoreCoupon coupon=fsStoreCouponService.selectFsStoreCouponById(publishParam.getCouponId());
        FsStoreCouponIssue issue=new FsStoreCouponIssue();
        issue.setCouponId(publishParam.getCouponId());
        issue.setCouponName(coupon.getTitle());
        issue.setCouponType(coupon.getType());
        issue.setStartTime(publishParam.getStartTime());
        issue.setLimitTime(publishParam.getLimitTime());
        issue.setTotalCount(publishParam.getTotalCount());
        issue.setRemainCount(0);
        issue.setIsPermanent(0);
        issue.setStatus(1);
        issue.setCreateTime(new Date());
        return toAjax( fsStoreCouponIssueService.insertFsStoreCouponIssue(issue));
    }

    @PreAuthorize("@ss.hasPermi('store:storeCoupon:batchPublish')")
    @PostMapping("/batchPublish")
    public R batchPublish(@RequestBody FsStoreCouponPublishParam publishParam)
    {

        List<FsStoreCoupon> list=fsStoreCouponService.selectFsStoreCouponByIds(publishParam.getIds());
        for (FsStoreCoupon coupon :list ){
            FsStoreCouponIssue issue=new FsStoreCouponIssue();
            issue.setCouponId(coupon.getCouponId());
            issue.setCouponName(coupon.getTitle());
            issue.setCouponType(coupon.getType());
            issue.setStartTime(publishParam.getStartTime());
            issue.setLimitTime(publishParam.getLimitTime());
            issue.setTotalCount(publishParam.getTotalCount());
            issue.setRemainCount(0);
            issue.setIsPermanent(0);
            issue.setStatus(1);
            issue.setCreateTime(new Date());
            fsStoreCouponIssueService.insertFsStoreCouponIssue(issue);
        }
        return R.ok();
    }
}
