package com.yijia.market.controller;

import java.util.List;

import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.system.service.ISysUserService;
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.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.market.domain.MarketCoupon;
import com.yijia.market.service.IMarketCouponService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 优惠劵管理Controller
 * 
 * @author yijia
 * @date 2020-12-22
 */
@RestController
@RequestMapping("/market/coupon")
public class MarketCouponController extends BaseController
{
    @Autowired
    private IMarketCouponService marketCouponService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 查询优惠劵管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MarketCoupon marketCoupon)
    {
//        LoginUser currentUser = SecurityUtils.getLoginUser();
//        boolean flag= sysUserService.getUserRole(currentUser);
//        if(!flag){
//            marketCoupon.setStationId(currentUser.getUser().getDeptId());
//        }
        startPage();
        List<MarketCoupon> list = marketCouponService.selectMarketCouponList(marketCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠劵管理列表
     */
    @Log(title = "优惠劵管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MarketCoupon marketCoupon)
    {
        List<MarketCoupon> list = marketCouponService.selectMarketCouponList(marketCoupon);
        ExcelUtil<MarketCoupon> util = new ExcelUtil<MarketCoupon>(MarketCoupon.class);
        return util.exportExcel(list, "coupon");
    }

    /**
     * 获取优惠劵管理详细信息
     */
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return AjaxResult.success(marketCouponService.selectMarketCouponById(couponId));
    }

    /**
     * 新增优惠劵管理
     */
    @Log(title = "优惠劵管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MarketCoupon marketCoupon)
    {
        return toAjax(marketCouponService.insertMarketCoupon(marketCoupon));
    }

    /**
     * 修改优惠劵管理
     */
    @Log(title = "优惠劵管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MarketCoupon marketCoupon)
    {
        return toAjax(marketCouponService.updateMarketCoupon(marketCoupon));
    }

    /**
     * 删除优惠劵管理
     */
    @Log(title = "优惠劵管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(marketCouponService.deleteMarketCouponByIds(couponIds));
    }
}
