package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2025-05-16
 */
@RestController
@RequestMapping("/system/Coupons")
public class CouponsController extends BaseController
{
    @Autowired
    private ICouponsService couponsService;
    @Autowired
    private ICouponUserService couponUserService;
    @Autowired
    private  IUsersService  usersService;
    @Autowired
    private   IServiceCateService serviceCateService;
    @Autowired
    private IServiceGoodsService serviceGoodsService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:list')")
    @GetMapping("/list")
    public TableDataInfo list(Coupons coupons)
    {
        startPage();
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        for (Coupons coupons1 : list) {
            coupons1.setLqjv(couponUserService.selectCountCouponUserbycouponId(coupons1.getId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupons coupons)
    {
        List<Coupons> list = couponsService.selectCouponsList(coupons);
        ExcelUtil<Coupons> util = new ExcelUtil<Coupons>(Coupons.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Coupons coupons=couponsService.selectCouponsById(id);
        if(coupons!=null){
            for(CouponUser couponUser :coupons.getCouponUserList()  ){
                Users users=usersService.selectUsersById(couponUser.getUid());
                ServiceCate serviceCate=serviceCateService.selectServiceCateById(couponUser.getCateId());
                ServiceGoods serviceGoods=serviceGoodsService.selectServiceGoodsById(couponUser.getProductId());
              if (users!=null){
                  couponUser.setUname(users.getName());
              }
              if (serviceCate!=null){
                  couponUser.setCatename(serviceCate.getTitle());
              }
              if (serviceGoods!=null){
                  couponUser.setProductname(serviceGoods.getTitle());
              }
            }
        }

        return success(coupons);
    }




    /**
     * 获取项目报价--工艺分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:query')")
    @GetMapping(value = "/getTypeList")
    public AjaxResult getTypeList()
    {
        return success(serviceCateService.selectServiceCateList(new ServiceCate()));
    }
    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coupons coupons)
    {
        return toAjax(couponsService.insertCoupons(coupons));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coupons coupons)
    {
        return toAjax(couponsService.updateCoupons(coupons));
    }
    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Coupons coupons)
    {
        Coupons newCoupons = couponsService.selectCouponsById(coupons.getId());
        newCoupons.setStatus(coupons.getStatus());
        return toAjax(couponsService.updateCoupons(newCoupons));
    }
    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('system:Coupons:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(couponsService.deleteCouponsByIds(ids));
    }



}
