Browse Source

服务号商城套餐维护

lighter 4 months ago
parent
commit
ea30b145be

+ 58 - 0
src/main/java/thyyxxk/webserver/controller/outpatient/wxmall/WxmallPackageController.java

@@ -0,0 +1,58 @@
+package thyyxxk.webserver.controller.outpatient.wxmall;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import thyyxxk.webserver.config.exception.ExceptionEnum;
+import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageCreation;
+import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageInquiry;
+import thyyxxk.webserver.entity.ResultVo;
+import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackage;
+import thyyxxk.webserver.service.outpatient.wxmall.WxmallPackageService;
+import thyyxxk.webserver.utils.ResultVoUtil;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/wxmallPackage")
+public class WxmallPackageController {
+
+    private final WxmallPackageService service;
+
+    @Autowired
+    public WxmallPackageController(WxmallPackageService service) {
+        this.service = service;
+    }
+
+    @PostMapping("/getPackagesByCondition")
+    public ResultVo<List<WxmallPackage>> getPackagesByCondition(@RequestBody PackageInquiry inquiry) {
+        List<WxmallPackage> list = service.getPackagesByCondition(inquiry);
+        if (null == list || list.isEmpty()) {
+            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
+        }
+        return ResultVoUtil.success(list);
+    }
+
+    @PostMapping("/createNewPackage")
+    public ResultVo<WxmallPackage> createNewPackage(PackageCreation creation) {
+        return ResultVoUtil.success(service.createNewPackage(creation));
+    }
+
+    @PostMapping("/createNewPackage2")
+    public ResultVo<WxmallPackage> createNewPackage2(@RequestBody PackageCreation creation) {
+        return ResultVoUtil.success(service.createNewPackage(creation));
+    }
+
+    @PostMapping("/updatePackageState")
+    public ResultVo<String> updatePackageState(@RequestBody WxmallPackage pkg) {
+        return ResultVoUtil.success(service.updatePackageState(pkg));
+    }
+
+    @GetMapping("/getPackageDetail")
+    public ResultVo<WxmallPackage> getPackageDetail(@RequestParam("id") String id) {
+        WxmallPackage pkg = service.getPackageDetail(id);
+        if (null == pkg) {
+            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
+        }
+        return ResultVoUtil.success(pkg);
+    }
+}

+ 12 - 0
src/main/java/thyyxxk/webserver/controller/outpatient/wxmall/dto/PackageCreation.java

@@ -0,0 +1,12 @@
+package thyyxxk.webserver.controller.outpatient.wxmall.dto;
+
+import lombok.Data;
+import org.springframework.web.multipart.MultipartFile;
+
+@Data
+public class PackageCreation {
+    private MultipartFile file;
+    private String fileName;
+    private String mainPackageJson;
+    private String packageItemListJson;
+}

+ 12 - 0
src/main/java/thyyxxk/webserver/controller/outpatient/wxmall/dto/PackageInquiry.java

@@ -0,0 +1,12 @@
+package thyyxxk.webserver.controller.outpatient.wxmall.dto;
+
+import lombok.Data;
+import thyyxxk.webserver.entity.outpatient.wxmall.PackageState;
+
+@Data
+public class PackageInquiry {
+    private String start;
+    private String end;
+    private PackageState state;
+    private String statement;
+}

+ 48 - 0
src/main/java/thyyxxk/webserver/dao/his/outpatient/WxmallPackageDao.java

@@ -0,0 +1,48 @@
+package thyyxxk.webserver.dao.his.outpatient;
+
+import org.apache.ibatis.annotations.*;
+import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageInquiry;
+import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackage;
+import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackageItem;
+
+import java.util.List;
+
+@Mapper
+public interface WxmallPackageDao {
+
+    @Select("select * from t_wxmall_package where create_time>=#{start} " +
+            "and create_time<=#{end} ${statement}")
+    List<WxmallPackage> getPackagesByCondition(PackageInquiry inquiry);
+
+    @Delete("delete from t_wxmall_package where id=#{id};" +
+            "delete from t_wxmall_package_item where package_id=#{id}")
+    void deletePackage(String id);
+
+    @Insert("insert into t_wxmall_package " +
+            "(id,name,description,price,thumb_path,create_time," +
+            "create_staff_code,create_staff_name) " +
+            "values " +
+            "(#{id},#{name},#{description},#{price},#{thumbPath},#{createTime}," +
+            "#{createStaffCode},#{createStaffName})")
+    void insertNewPackage(WxmallPackage wxmallPackage);
+
+    @Insert("<script>" +
+            "insert into t_wxmall_package_item " +
+            "(package_id,his_code,his_name,price,quantity) " +
+            "values " +
+            "<foreach collection='list' item='item' separator=','>" +
+            "(#{item.packageId},#{item.hisCode},#{item.hisName}," +
+            "#{item.price},#{item.quantity})" +
+            "</foreach>" +
+            "</script>")
+    void insertPackageItem(List<WxmallPackageItem> list);
+
+    @Update("update t_wxmall_package set state=#{state} where id=#{id}")
+    void updatePackageState(WxmallPackage pkg);
+
+    @Select("select * from t_wxmall_package where id=#{id}")
+    WxmallPackage getPackageById(String id);
+
+    @Select("select * from t_wxmall_package_item where package_id=#{packageId}")
+    List<WxmallPackageItem> getPackageItems(String packageId);
+}

+ 17 - 0
src/main/java/thyyxxk/webserver/entity/outpatient/wxmall/PackageState.java

@@ -0,0 +1,17 @@
+package thyyxxk.webserver.entity.outpatient.wxmall;
+
+public enum PackageState {
+    /**
+     * 套餐维护状态:
+     * 已上架;已下架
+     * */
+    ACTIVATED,
+    DEACTIVATED,
+
+    /**
+     * 套餐使用状态:
+     * 可使用;已用完
+     * */
+    USABLE,
+    RAN_OUT
+}

+ 21 - 0
src/main/java/thyyxxk/webserver/entity/outpatient/wxmall/WxmallPackage.java

@@ -0,0 +1,21 @@
+package thyyxxk.webserver.entity.outpatient.wxmall;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+
+@Data
+public class WxmallPackage {
+    private String id;
+    private String name;
+    private String description;
+    private BigDecimal price;
+    private String thumbPath;
+    private Date createTime;
+    private String createStaffCode;
+    private String createStaffName;
+    private PackageState state;
+    private List<WxmallPackageItem> items;
+}

+ 14 - 0
src/main/java/thyyxxk/webserver/entity/outpatient/wxmall/WxmallPackageItem.java

@@ -0,0 +1,14 @@
+package thyyxxk.webserver.entity.outpatient.wxmall;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+@Data
+public class WxmallPackageItem {
+    private String packageId;
+    private String hisCode;
+    private String hisName;
+    private BigDecimal price;
+    private Integer quantity;
+}

+ 20 - 0
src/main/java/thyyxxk/webserver/entity/outpatient/wxmall/WxmallPackagePurchased.java

@@ -0,0 +1,20 @@
+package thyyxxk.webserver.entity.outpatient.wxmall;
+
+import lombok.Data;
+
+import java.math.BigDecimal;
+import java.util.Date;
+
+@Data
+public class WxmallPackagePurchased {
+    private String id;
+    private String patNo;
+    private String wxTradeNo;
+    private String packageId;
+    private String packageName;
+    private BigDecimal purchasePrice;
+    private BigDecimal couponAmt;
+    private BigDecimal cashpayAmt;
+    private Date purchaseTime;
+    private PackageState state;
+}

+ 18 - 0
src/main/java/thyyxxk/webserver/entity/outpatient/wxmall/WxmallPackagePurchasedItem.java

@@ -0,0 +1,18 @@
+package thyyxxk.webserver.entity.outpatient.wxmall;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class WxmallPackagePurchasedItem {
+    private String id;
+    private String wxTradeNo;
+    private String patNo;
+    private String packageId;
+    private String hisCode;
+    private String hisName;
+    private Integer quantity;
+    private Integer usableQuantity;
+    private Date lastUseTime;
+}

+ 18 - 0
src/main/java/thyyxxk/webserver/entity/outpatient/wxmall/WxmallPackageUsed.java

@@ -0,0 +1,18 @@
+package thyyxxk.webserver.entity.outpatient.wxmall;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class WxmallPackageUsed {
+    private String id;
+    private String patNo;
+    private Integer times;
+    private String wxTradeNo;
+    private String packageId;
+    private String packageItemId;
+    private Integer useQuantity;
+    private Date useTime;
+    private String doctorCode;
+}

+ 150 - 0
src/main/java/thyyxxk/webserver/service/outpatient/wxmall/WxmallPackageService.java

@@ -0,0 +1,150 @@
+package thyyxxk.webserver.service.outpatient.wxmall;
+
+import com.alibaba.fastjson.JSON;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.*;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestClientException;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
+import thyyxxk.webserver.config.exception.BizException;
+import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageCreation;
+import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageInquiry;
+import thyyxxk.webserver.dao.his.outpatient.WxmallPackageDao;
+import thyyxxk.webserver.entity.outpatient.wxmall.PackageState;
+import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackage;
+import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackageItem;
+import thyyxxk.webserver.service.redislike.RedisLikeService;
+import thyyxxk.webserver.utils.SnowFlakeId;
+import thyyxxk.webserver.utils.StringUtil;
+import thyyxxk.webserver.utils.TokenUtil;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Date;
+import java.util.List;
+
+@Slf4j
+@Service
+public class WxmallPackageService {
+    private final WxmallPackageDao dao;
+    private final RedisLikeService redis;
+
+    @Autowired
+    public WxmallPackageService(WxmallPackageDao dao, RedisLikeService redis) {
+        this.dao = dao;
+        this.redis = redis;
+    }
+
+    public List<WxmallPackage> getPackagesByCondition(PackageInquiry inquiry) {
+        PackageState state = inquiry.getState();
+        if (null == state) {
+            inquiry.setStatement("");
+        } else {
+            inquiry.setStatement(" and state='" + state + "'");
+        }
+        return dao.getPackagesByCondition(inquiry);
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public WxmallPackage createNewPackage(PackageCreation creation) {
+
+        WxmallPackage mainPackage = JSON.parseObject(creation.getMainPackageJson(), WxmallPackage.class);
+        List<WxmallPackageItem> list = JSON.parseArray(creation.getPackageItemListJson(), WxmallPackageItem.class);
+
+        String packageId = mainPackage.getId();
+        if (StringUtil.isBlank(packageId)) {
+            packageId = SnowFlakeId.instance().nextId();
+        } else {
+            dao.deletePackage(packageId);
+        }
+        String thumbPath = mainPackage.getThumbPath();
+        if (creation.getFile() != null) {
+            creation.setFileName(packageId);
+            thumbPath = saveThumbImageToDisk(creation);
+        }
+
+        String userCode = TokenUtil.getInstance().getTokenUserId();
+        String userName = redis.getEmployeeName(userCode);
+        mainPackage.setId(packageId);
+        mainPackage.setThumbPath(thumbPath);
+        mainPackage.setCreateStaffCode(userCode);
+        mainPackage.setCreateStaffName(userName);
+        mainPackage.setCreateTime(new Date());
+        mainPackage.setState(PackageState.ACTIVATED);
+
+        BigDecimal cost = BigDecimal.ZERO;
+        for (WxmallPackageItem item : list) {
+            item.setPackageId(packageId);
+            cost = cost.add(item.getPrice().multiply(new BigDecimal(item.getQuantity())));
+        }
+        mainPackage.setPrice(cost.setScale(2, RoundingMode.HALF_UP));
+
+        dao.insertNewPackage(mainPackage);
+        dao.insertPackageItem(list);
+
+        return mainPackage;
+    }
+
+    private String saveThumbImageToDisk(PackageCreation creation) {
+        if (null == creation.getFile() || creation.getFile().isEmpty()) {
+            return null;
+        }
+
+        try {
+            String url = "https://staticweb.hnthyy.cn/htmlToImage/saveImg/saveThumbImageToDisk";
+            ByteArrayResource fileResource = new ByteArrayResource(creation.getFile().getBytes()) {
+                @Override
+                public String getFilename() {
+                    return creation.getFile().getOriginalFilename();
+                }
+            };
+
+            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
+            body.add("file", fileResource);
+            body.add("fileName", creation.getFileName());
+
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
+
+            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
+            RestTemplate template = new RestTemplate();
+            ResponseEntity<String> res = template.exchange(url, HttpMethod.POST, request, String.class);
+            return res.getBody();
+        } catch (IOException e) {
+            log.error("文件读取失败", e);
+            return null;
+        } catch (RestClientException e) {
+            log.error("API调用失败", e);
+            return null;
+        }
+    }
+
+    public String updatePackageState(WxmallPackage pkg) {
+        dao.updatePackageState(pkg);
+        return "操作成功";
+    }
+
+    public WxmallPackage getPackageDetail(String packageId) {
+        WxmallPackage pkg = dao.getPackageById(packageId);
+        if (null == pkg) {
+            return null;
+        }
+        List<WxmallPackageItem> items = dao.getPackageItems(packageId);
+        pkg.setItems(items);
+        return pkg;
+    }
+}