package com.fs.company.controller.store;

import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.domain.FsUserCoupon;
import com.fs.his.param.FsUserCouponParam;
import com.fs.his.param.FsUserCouponSendParam;
import com.fs.his.service.IFsUserCouponService;
import com.fs.his.vo.FsUserCouponListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 会员优惠券Controller
 *
 * @author fs
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/store/userCoupon")
public class FsUserCouponController extends BaseController
{
    @Autowired
    private IFsUserCouponService fsUserCouponService;

    /**
     * 查询会员优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('his:userCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserCouponParam fsUserCoupon)
    {
        startPage();
        List<FsUserCouponListVO> list = fsUserCouponService.selectFsUserCouponListVO(fsUserCoupon);
        return getDataTable(list);
    }

    @GetMapping("/getList")
    public TableDataInfo getList(FsUserCouponParam fsUserCoupon)
    {
        startPage();
        List<FsUserCouponListVO> list = fsUserCouponService.selectFsUserCouponListVO(fsUserCoupon);
        for (FsUserCouponListVO fsUserCouponListVO : list) {
            if (fsUserCouponListVO.getPhone()!=null){
                fsUserCouponListVO.setPhone(fsUserCouponListVO.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出会员优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('his:userCoupon:export')")
    @Log(title = "会员优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserCoupon fsUserCoupon)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        logger.info("导出优惠券:"+ loginUser.getUser().getUserId());
        List<FsUserCoupon> list = fsUserCouponService.selectFsUserCouponList(fsUserCoupon);
        ExcelUtil<FsUserCoupon> util = new ExcelUtil<FsUserCoupon>(FsUserCoupon.class);
        return util.exportExcel(list, "会员优惠券数据");
    }

    /**
     * 获取会员优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:userCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserCouponService.selectFsUserCouponById(id));
    }

    /**
     * 新增会员优惠券
     */
    @PreAuthorize("@ss.hasPermi('his:userCoupon:add')")
    @Log(title = "会员优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserCoupon fsUserCoupon)
    {
        return toAjax(fsUserCouponService.insertFsUserCoupon(fsUserCoupon));
    }

    /**
     * 发送会员优惠券
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('his:userCoupon:send')")
    @Log(title = "发送会员优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/sendCoupon")
    public AjaxResult sendCoupon(@RequestBody FsUserCouponSendParam fsUserCoupon)
    {
        return toAjax(fsUserCouponService.sendFsUserCoupon(fsUserCoupon));
    }


    /**
     * 修改会员优惠券
     */
    @PreAuthorize("@ss.hasPermi('his:userCoupon:edit')")
    @Log(title = "会员优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserCoupon fsUserCoupon)
    {
        return toAjax(fsUserCouponService.updateFsUserCoupon(fsUserCoupon));
    }

    /**
     * 删除会员优惠券
     */
    @PreAuthorize("@ss.hasPermi('his:userCoupon:remove')")
    @Log(title = "会员优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsUserCouponService.deleteFsUserCouponByIds(ids));
    }
}
