From 8ae8a4a652bc140b78f2a4e7f4f766826de74260 Mon Sep 17 00:00:00 2001
From: YunaiV <zhijiantianya@gmail.com>
Date: Wed, 2 Nov 2022 22:44:37 +0800
Subject: [PATCH] =?UTF-8?q?promotion=EF=BC=9A=E5=AE=8C=E5=96=84=E4=BC=98?=
 =?UTF-8?q?=E6=83=A0=E5=8A=B5=E6=A8=A1=E6=9D=BF=E7=9A=84=E5=8D=95=E5=85=83?=
 =?UTF-8?q?=E6=B5=8B=E8=AF=95=E3=80=81=E7=A6=81=E7=94=A8=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../coupon/CouponTemplateController.java      | 13 ++++--
 .../vo/CouponTemplateUpdateStatusReqVO.java   | 24 +++++++++++
 .../service/coupon/CouponTemplateService.java |  8 ++++
 .../coupon/CouponTemplateServiceImpl.java     |  8 ++++
 .../coupon/CouponTemplateServiceImplTest.java | 41 ++++++++++++-------
 .../src/test/resources/sql/clean.sql          |  1 +
 .../src/test/resources/sql/create_tables.sql  | 31 +++++++++++++-
 .../vo/role/RoleUpdateStatusReqVO.java        |  4 +-
 .../src/api/mall/promotion/couponTemplate.js  | 13 ++++++
 .../mall/promotion/couponTemplate/index.vue   | 33 +++++++++++++--
 .../src/views/system/role/index.vue           |  3 +-
 11 files changed, 153 insertions(+), 26 deletions(-)
 create mode 100644 yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/CouponTemplateUpdateStatusReqVO.java

diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/CouponTemplateController.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/CouponTemplateController.java
index 4f1d51c61..ce153ad70 100755
--- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/CouponTemplateController.java
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/CouponTemplateController.java
@@ -2,10 +2,7 @@ package cn.iocoder.yudao.module.promotion.controller.admin.coupon;
 
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
-import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTemplateCreateReqVO;
-import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTemplatePageReqVO;
-import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTemplateRespVO;
-import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTemplateUpdateReqVO;
+import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.*;
 import cn.iocoder.yudao.module.promotion.convert.coupon.CouponTemplateConvert;
 import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO;
 import cn.iocoder.yudao.module.promotion.service.coupon.CouponTemplateService;
@@ -45,6 +42,14 @@ public class CouponTemplateController {
         return success(true);
     }
 
+    @PutMapping("/update-status")
+    @ApiOperation("更新优惠劵模板状态")
+    @PreAuthorize("@ss.hasPermission('promotion:coupon-template:update')")
+    public CommonResult<Boolean> updateCouponTemplateStatus(@Valid @RequestBody CouponTemplateUpdateStatusReqVO reqVO) {
+        couponTemplateService.updateCouponTemplateStatus(reqVO.getId(), reqVO.getStatus());
+        return success(true);
+    }
+
     @DeleteMapping("/delete")
     @ApiOperation("删除优惠劵模板")
     @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/CouponTemplateUpdateStatusReqVO.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/CouponTemplateUpdateStatusReqVO.java
new file mode 100644
index 000000000..86ecaae1a
--- /dev/null
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/admin/coupon/vo/CouponTemplateUpdateStatusReqVO.java
@@ -0,0 +1,24 @@
+package cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo;
+
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
+import cn.iocoder.yudao.framework.common.validation.InEnum;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+
+@ApiModel("管理后台 - 优惠劵模板更新状态 Request VO")
+@Data
+public class CouponTemplateUpdateStatusReqVO {
+
+    @ApiModelProperty(value = "优惠劵模板编号", required = true, example = "1024")
+    @NotNull(message = "优惠劵模板编号不能为空")
+    private Long id;
+
+    @ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举")
+    @NotNull(message = "状态不能为空")
+    @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
+    private Integer status;
+
+}
diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateService.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateService.java
index a1fc14d13..35da0f3c0 100755
--- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateService.java
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateService.java
@@ -30,6 +30,14 @@ public interface CouponTemplateService {
      */
     void updateCouponTemplate(@Valid CouponTemplateUpdateReqVO updateReqVO);
 
+    /**
+     * 更新优惠劵模板的状态
+     *
+     * @param id 编号
+     * @param status 状态
+     */
+    void updateCouponTemplateStatus(Long id, Integer status);
+
     /**
      * 删除优惠劵模板
      *
diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java
index 928c9c15d..63b25b05b 100755
--- a/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImpl.java
@@ -52,6 +52,14 @@ public class CouponTemplateServiceImpl implements CouponTemplateService {
         couponTemplateMapper.updateById(updateObj);
     }
 
+    @Override
+    public void updateCouponTemplateStatus(Long id, Integer status) {
+        // 校验存在
+        validateCouponTemplateExists(id);
+        // 更新
+        couponTemplateMapper.updateById(new CouponTemplateDO().setId(id).setStatus(status));
+    }
+
     @Override
     public void deleteCouponTemplate(Long id) {
         // 校验存在
diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImplTest.java b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImplTest.java
index 8a87fb584..60a96ce75 100755
--- a/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImplTest.java
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponTemplateServiceImplTest.java
@@ -1,5 +1,6 @@
 package cn.iocoder.yudao.module.promotion.service.coupon;
 
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
 import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTemplateCreateReqVO;
@@ -7,13 +8,17 @@ import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTempla
 import cn.iocoder.yudao.module.promotion.controller.admin.coupon.vo.CouponTemplateUpdateReqVO;
 import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponTemplateDO;
 import cn.iocoder.yudao.module.promotion.dal.mysql.coupon.CouponTemplateMapper;
-import org.junit.jupiter.api.Disabled;
+import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum;
+import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
+import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTemplateValidityTypeEnum;
 import org.junit.jupiter.api.Test;
 import org.springframework.context.annotation.Import;
 
 import javax.annotation.Resource;
 import java.util.Date;
 
+import static cn.hutool.core.util.RandomUtil.randomEle;
+import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime;
 import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
 import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
@@ -39,7 +44,10 @@ public class CouponTemplateServiceImplTest extends BaseDbUnitTest {
     @Test
     public void testCreateCouponTemplate_success() {
         // 准备参数
-        CouponTemplateCreateReqVO reqVO = randomPojo(CouponTemplateCreateReqVO.class);
+        CouponTemplateCreateReqVO reqVO = randomPojo(CouponTemplateCreateReqVO.class,
+                o -> o.setProductScope(randomEle(PromotionProductScopeEnum.values()).getScope())
+                        .setValidityType(randomEle(CouponTemplateValidityTypeEnum.values()).getType())
+                        .setDiscountType(randomEle(PromotionDiscountTypeEnum.values()).getType()));
 
         // 调用
         Long couponTemplateId = couponTemplateService.createCouponTemplate(reqVO);
@@ -58,6 +66,10 @@ public class CouponTemplateServiceImplTest extends BaseDbUnitTest {
         // 准备参数
         CouponTemplateUpdateReqVO reqVO = randomPojo(CouponTemplateUpdateReqVO.class, o -> {
             o.setId(dbCouponTemplate.getId()); // 设置更新的 ID
+            // 其它通用字段
+            o.setProductScope(randomEle(PromotionProductScopeEnum.values()).getScope())
+                    .setValidityType(randomEle(CouponTemplateValidityTypeEnum.values()).getType())
+                    .setDiscountType(randomEle(PromotionDiscountTypeEnum.values()).getType());
         });
 
         // 调用
@@ -100,30 +112,29 @@ public class CouponTemplateServiceImplTest extends BaseDbUnitTest {
     }
 
     @Test
-    @Disabled  // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
     public void testGetCouponTemplatePage() {
        // mock 数据
        CouponTemplateDO dbCouponTemplate = randomPojo(CouponTemplateDO.class, o -> { // 等会查询到
-           o.setName(null);
-           o.setStatus(null);
-           o.setDiscountType(null);
-           o.setCreateTime(null);
+           o.setName("芋艿");
+           o.setStatus(CommonStatusEnum.ENABLE.getStatus());
+           o.setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType());
+           o.setCreateTime(buildTime(2022, 2, 2));
        });
        couponTemplateMapper.insert(dbCouponTemplate);
        // 测试 name 不匹配
-       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setName(null)));
+       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setName("土豆")));
        // 测试 status 不匹配
-       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setStatus(null)));
+       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
        // 测试 type 不匹配
-       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setDiscountType(null)));
+       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setDiscountType(PromotionDiscountTypeEnum.PRICE.getType())));
        // 测试 createTime 不匹配
-       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setCreateTime(null)));
+       couponTemplateMapper.insert(cloneIgnoreId(dbCouponTemplate, o -> o.setCreateTime(buildTime(2022, 1, 1))));
        // 准备参数
        CouponTemplatePageReqVO reqVO = new CouponTemplatePageReqVO();
-       reqVO.setName(null);
-       reqVO.setStatus(null);
-       reqVO.setDiscountType(null);
-       reqVO.setCreateTime((new Date[]{}));
+       reqVO.setName("芋艿");
+       reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
+       reqVO.setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType());
+       reqVO.setCreateTime((new Date[]{buildTime(2022, 2, 1), buildTime(2022, 2, 3)}));
 
        // 调用
        PageResult<CouponTemplateDO> pageResult = couponTemplateService.getCouponTemplatePage(reqVO);
diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/clean.sql b/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/clean.sql
index abad7c069..8fdec6cbe 100644
--- a/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/clean.sql
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/clean.sql
@@ -1 +1,2 @@
 DELETE FROM "market_activity";
+DELETE FROM "promotion_coupon_template";
diff --git a/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql b/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql
index 3f3ce3c4d..188a86c67 100644
--- a/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql
+++ b/yudao-module-mall/yudao-module-promotion-biz/src/test/resources/sql/create_tables.sql
@@ -16,4 +16,33 @@ CREATE TABLE IF NOT EXISTS "market_activity" (
     "deleted" bit NOT NULL DEFAULT FALSE,
     "tenant_id" bigint(20) NOT NULL,
     PRIMARY KEY ("id")
-    ) COMMENT '促销活动';
\ No newline at end of file
+    ) COMMENT '促销活动';
+
+CREATE TABLE IF NOT EXISTS "promotion_coupon_template" (
+   "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
+   "name" varchar NOT NULL,
+   "status" int NOT NULL,
+   "total_count" int NOT NULL,
+   "take_limit_count" int NOT NULL,
+   "take_type" int NOT NULL,
+   "use_price" int NOT NULL,
+   "product_scope" int NOT NULL,
+   "product_spu_ids" varchar,
+   "validity_type" int NOT NULL,
+   "valid_start_time" datetime,
+   "valid_end_time" datetime,
+   "fixed_start_term" int,
+   "fixed_end_term" int,
+   "discount_type" int NOT NULL,
+   "discount_percent" int,
+   "discount_price" int,
+   "discount_limit_price" int,
+   "take_count" int NOT NULL DEFAULT 0,
+   "use_count" int NOT NULL DEFAULT 0,
+   "creator" varchar DEFAULT '',
+   "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
+   "updater" varchar DEFAULT '',
+   "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+   "deleted" bit NOT NULL DEFAULT FALSE,
+   PRIMARY KEY ("id")
+) COMMENT '优惠劵模板';
diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleUpdateStatusReqVO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleUpdateStatusReqVO.java
index 708586b82..790accd92 100644
--- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleUpdateStatusReqVO.java
+++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleUpdateStatusReqVO.java
@@ -1,5 +1,7 @@
 package cn.iocoder.yudao.module.system.controller.admin.permission.vo.role;
 
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
+import cn.iocoder.yudao.framework.common.validation.InEnum;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
@@ -16,7 +18,7 @@ public class RoleUpdateStatusReqVO {
 
     @ApiModelProperty(value = "状态", required = true, example = "1", notes = "见 CommonStatusEnum 枚举")
     @NotNull(message = "状态不能为空")
-//    @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
+    @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}")
     private Integer status;
 
 }
diff --git a/yudao-ui-admin/src/api/mall/promotion/couponTemplate.js b/yudao-ui-admin/src/api/mall/promotion/couponTemplate.js
index 67d821cf0..29f81a19c 100755
--- a/yudao-ui-admin/src/api/mall/promotion/couponTemplate.js
+++ b/yudao-ui-admin/src/api/mall/promotion/couponTemplate.js
@@ -18,6 +18,19 @@ export function updateCouponTemplate(data) {
   })
 }
 
+// 更新优惠劵模板的状态
+export function updateCouponTemplateStatus(id, status) {
+  const data = {
+    id,
+    status
+  }
+  return request({
+    url: '/promotion/coupon-template/update-status',
+    method: 'put',
+    data: data
+  })
+}
+
 // 删除优惠劵模板
 export function deleteCouponTemplate(id) {
   return request({
diff --git a/yudao-ui-admin/src/views/mall/promotion/couponTemplate/index.vue b/yudao-ui-admin/src/views/mall/promotion/couponTemplate/index.vue
index 8dd56917b..2b25bfa80 100755
--- a/yudao-ui-admin/src/views/mall/promotion/couponTemplate/index.vue
+++ b/yudao-ui-admin/src/views/mall/promotion/couponTemplate/index.vue
@@ -52,7 +52,7 @@
       <el-table-column label="有效期限" align="center" prop="validityType" width="180" :formatter="validityTypeFormat" />
       <el-table-column label="状态" align="center" prop="status">
         <template slot-scope="scope">
-          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
+          <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" @change="handleStatusChange(scope.row)"/>
         </template>
       </el-table-column>
       <el-table-column label="创建时间" align="center" prop="createTime" width="180">
@@ -156,10 +156,23 @@
 </template>
 
 <script>
-import { createCouponTemplate, updateCouponTemplate, deleteCouponTemplate, getCouponTemplate, getCouponTemplatePage } from "@/api/mall/promotion/couponTemplate";
-import { CouponTemplateValidityTypeEnum, PromotionDiscountTypeEnum, PromotionProductScopeEnum} from "@/utils/constants";
+import {
+  createCouponTemplate,
+  updateCouponTemplate,
+  deleteCouponTemplate,
+  getCouponTemplate,
+  getCouponTemplatePage,
+  updateCouponTemplateStatus
+} from "@/api/mall/promotion/couponTemplate";
+import {
+  CommonStatusEnum,
+  CouponTemplateValidityTypeEnum,
+  PromotionDiscountTypeEnum,
+  PromotionProductScopeEnum
+} from "@/utils/constants";
 import { getSpuSimpleList } from "@/api/mall/product/spu";
 import { parseTime } from "@/utils/ruoyi";
+import {changeRoleStatus} from "@/api/system/role";
 
 export default {
   name: "CouponTemplate",
@@ -331,6 +344,20 @@ export default {
         });
       });
     },
+    /** 优惠劵模板状态修改 */
+    handleStatusChange(row) {
+      // 此时,row 已经变成目标状态了,所以可以直接提交请求和提示
+      let text = row.status === CommonStatusEnum.ENABLE ? "启用" : "停用";
+      this.$modal.confirm('确认要"' + text + '""' + row.name + '"优惠劵吗?').then(function() {
+        return updateCouponTemplateStatus(row.id, row.status);
+      }).then(() => {
+        this.$modal.msgSuccess(text + "成功");
+      }).catch(function() {
+        // 异常时,需要将 row.status 状态重置回之前的
+        row.status = row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE
+            : CommonStatusEnum.ENABLE;
+      });
+    },
     /** 删除按钮操作 */
     handleDelete(row) {
       const id = row.id;
diff --git a/yudao-ui-admin/src/views/system/role/index.vue b/yudao-ui-admin/src/views/system/role/index.vue
index 4e399ce8c..7f945ed38 100644
--- a/yudao-ui-admin/src/views/system/role/index.vue
+++ b/yudao-ui-admin/src/views/system/role/index.vue
@@ -50,7 +50,7 @@
       <el-table-column label="显示顺序" prop="sort" width="100" />
       <el-table-column label="状态" align="center" width="100">
         <template slot-scope="scope">
-          <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
+          <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" @change="handleStatusChange(scope.row)"/>
         </template>
       </el-table-column>
       <el-table-column label="创建时间" align="center" prop="createTime" width="180">
@@ -167,7 +167,6 @@
 import {
   addRole,
   changeRoleStatus,
-  dataScope,
   delRole,
   exportRole,
   getRole,