package com.fs.his.controller;

import java.util.List;

import com.fs.common.annotation.RepeatSubmit;
import com.fs.his.param.FsUserCouponParam;
import com.fs.his.param.FsUserCouponSendParam;
import com.fs.his.vo.FsFollowExcelListVO;
import com.fs.his.vo.FsUserCouponListVO;
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.FsUserCoupon;
import com.fs.his.service.IFsUserCouponService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

import static com.fs.his.utils.PhoneUtil.decryptAutoPhoneMk;

/**
 * 会员优惠券Controller
 *
 * @author fs
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/his/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);
        for (FsUserCouponListVO vo : list) {
            vo.setPhone(decryptAutoPhoneMk(vo.getPhone()));
        }
        return getDataTable(list);
    }

    @GetMapping("/getList")
    public TableDataInfo getList(FsUserCouponParam fsUserCoupon)
    {
        startPage();
        List<FsUserCouponListVO> list = fsUserCouponService.selectFsUserCouponListVO(fsUserCoupon);
        for (FsUserCouponListVO vo : list) {
            vo.setPhone(vo.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(FsUserCouponParam fsUserCoupon)
    {
        List<FsUserCouponListVO> list = fsUserCouponService.selectFsUserCouponListVO(fsUserCoupon);
        for (FsUserCouponListVO vo : list) {
            vo.setPhone(vo.getPhone().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
        }
        ExcelUtil<FsUserCouponListVO> util = new ExcelUtil<FsUserCouponListVO>(FsUserCouponListVO.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));
    }
}
