199 lines
6.9 KiB
Java
199 lines
6.9 KiB
Java
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));
|
||
}
|
||
|
||
/**
|
||
* 根据优惠券ID查询userIds对应的用户列表,只返回name、phone、userId字段
|
||
*/
|
||
@PreAuthorize("@ss.hasPermi('system:Coupons:query')")
|
||
@GetMapping("/userListByCoupon/{id}")
|
||
public AjaxResult getUserListByCoupon(@PathVariable("id") Long id) {
|
||
Coupons coupon = couponsService.selectCouponsById(id);
|
||
if (coupon == null || coupon.getUserIds() == null) {
|
||
return error("优惠券不存在");
|
||
}
|
||
List<Long> userIdList;
|
||
try {
|
||
// 假设userIds存储为JSON数组字符串
|
||
userIdList = com.alibaba.fastjson2.JSON.parseArray(coupon.getUserIds(), Long.class);
|
||
} catch (Exception e) {
|
||
// 如果不是JSON格式,尝试逗号分隔
|
||
userIdList = new java.util.ArrayList<>();
|
||
for (String s : coupon.getUserIds().split(",")) {
|
||
try {
|
||
userIdList.add(Long.parseLong(s.trim()));
|
||
} catch (Exception ignore) {}
|
||
}
|
||
}
|
||
if (userIdList.isEmpty()) {
|
||
return error("优惠券不存在");
|
||
}
|
||
// 查询用户列表
|
||
List<Users> usersList = usersService.selectUsersByIds(userIdList);
|
||
// 只返回name、phone、userId字段
|
||
List<java.util.Map<String, Object>> simpleList = new java.util.ArrayList<>();
|
||
for (Users user : usersList) {
|
||
java.util.Map<String, Object> map = new java.util.HashMap<>();
|
||
map.put("userId", user.getId());
|
||
map.put("name", user.getName());
|
||
map.put("phone", user.getPhone());
|
||
simpleList.add(map);
|
||
}
|
||
return success(simpleList);
|
||
}
|
||
|
||
}
|