diff --git a/src/main/java/com/xkrs/controller/SettingController.java b/src/main/java/com/xkrs/controller/SettingController.java new file mode 100644 index 0000000..dfeb7fd --- /dev/null +++ b/src/main/java/com/xkrs/controller/SettingController.java @@ -0,0 +1,46 @@ +package com.xkrs.controller; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.dao.SettingDao; +import com.xkrs.model.entity.SettingEntity; +import com.xkrs.model.qo.SettingQo; +import org.apache.http.util.TextUtils; +import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Locale; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +/** + * 设置审核开关状态 + */ +@RestController +public class SettingController { + + @Resource + private SettingDao settingDao; + + @PostMapping("/updateswitchstate") + public String updateSwitchState(@RequestBody SettingQo settingQo) { + Locale locale = LocaleContextHolder.getLocale(); + if (TextUtils.isEmpty(settingQo.getSwitchState())) { + return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "switchState参数不能为空", locale); + } + List settingEntityList = settingDao.findAll(); + if (settingEntityList.isEmpty()) { + SettingEntity settingEntity = new SettingEntity(); + settingEntity.setSwitchState(settingQo.getSwitchState()); + settingDao.save(settingEntity); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); + } + SettingEntity settingEntity = settingEntityList.get(0); + settingDao.updateSwitchStateById(settingEntity.getId(), settingQo.getSwitchState()); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); + } + +} diff --git a/src/main/java/com/xkrs/dao/SettingDao.java b/src/main/java/com/xkrs/dao/SettingDao.java new file mode 100644 index 0000000..5be593a --- /dev/null +++ b/src/main/java/com/xkrs/dao/SettingDao.java @@ -0,0 +1,20 @@ +package com.xkrs.dao; + +import com.xkrs.model.entity.SettingEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Component; + +/** + * @author xkrs + */ +@Component +public interface SettingDao extends JpaRepository, JpaSpecificationExecutor { + + @Modifying(clearAutomatically = true) + @Query(value = "UPDATE setting SET switch_state = ?2 where id = ?1", nativeQuery = true) + void updateSwitchStateById(Long id, String switchState); + +} diff --git a/src/main/java/com/xkrs/model/entity/SettingEntity.java b/src/main/java/com/xkrs/model/entity/SettingEntity.java new file mode 100644 index 0000000..caab61d --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/SettingEntity.java @@ -0,0 +1,50 @@ +package com.xkrs.model.entity; + +import javax.persistence.*; +import java.io.Serializable; + +/** + * @author xkrs + */ +@Entity +@Table(name = "setting") +public class SettingEntity implements Serializable { + + /** + * 主键id + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "setting_seq_gen") + @SequenceGenerator(name = "setting_seq_gen", sequenceName = "setting_id_seq", allocationSize = 1) + private Long id; + + /** + * 0:关,1:开 + */ + @Column(length = 64, columnDefinition = "varchar(64)") + private String switchState; + + public SettingEntity() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getSwitchState() { + return switchState; + } + + public void setSwitchState(String switchState) { + this.switchState = switchState; + } + + @Override + public String toString() { + return "SettingEntity{" + "id=" + id + ", switchState='" + switchState + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/qo/SettingQo.java b/src/main/java/com/xkrs/model/qo/SettingQo.java new file mode 100644 index 0000000..b00a5dd --- /dev/null +++ b/src/main/java/com/xkrs/model/qo/SettingQo.java @@ -0,0 +1,27 @@ +package com.xkrs.model.qo; + +import com.xkrs.model.validation.SettingQoUpdate; + +import javax.validation.constraints.NotNull; + +public class SettingQo { + + @NotNull(message = "{Setting.switchState.blank}", groups = {SettingQoUpdate.class}) + private String switchState; + + public SettingQo() { + } + + public String getSwitchState() { + return switchState; + } + + public void setSwitchState(String switchState) { + this.switchState = switchState; + } + + @Override + public String toString() { + return "SettingQo{" + "switchState='" + switchState + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/validation/SettingQoUpdate.java b/src/main/java/com/xkrs/model/validation/SettingQoUpdate.java new file mode 100644 index 0000000..7d05642 --- /dev/null +++ b/src/main/java/com/xkrs/model/validation/SettingQoUpdate.java @@ -0,0 +1,4 @@ +package com.xkrs.model.validation; + +public interface SettingQoUpdate { +}