Explorar el Código

定时任务转移

lighter hace 7 meses
padre
commit
829f00b479

+ 30 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/dao/ClockinDao.java

@@ -0,0 +1,30 @@
+package org.thyy.scheduled.dao;
+
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Select;
+import org.thyy.scheduled.entity.clockin.Clockin;
+
+import java.util.List;
+
+@Mapper
+public interface ClockinDao {
+    @Select("select ltrim(rtrim(code_rs)) from a_employee_mi with(nolock) " +
+            "where isnull(del_flag, 0)=0 and isnull(dept_code,'')!='3100000'")
+    List<String> getAllEmployeeExceptItDept();
+
+    @Select("select ltrim(rtrim(code_rs)) from a_employee_mi with(nolock) where " +
+            "isnull(del_flag, 0)=0 and dept_code='3100000' and code_rs!='0000'")
+    List<String> getAllEmployeeInItDept();
+
+    @Insert("<script>" +
+            "insert into clock_in (groupname, checkin_type, exception_type, checkin_time, " +
+            "location_title, location_detail, userid, checkin_date, checkin_hour, notes) values " +
+            "<foreach collection='list' item='dk' open='' separator=',' close=''>" +
+            "(#{dk.groupname}, #{dk.checkinType}, #{dk.exceptionType}, #{dk.checkinTime}, " +
+            "#{dk.locationTitle}, #{dk.locationDetail}, #{dk.userid}, #{dk.checkinDate}, " +
+            "#{dk.checkinHour}, #{dk.notes})" +
+            "</foreach>" +
+            "</script>")
+    void insertIntoTable(List<Clockin> list);
+}

+ 77 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/dao/MainBusinessDo.java

@@ -1,7 +1,13 @@
 package org.thyy.scheduled.dao;
 
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Update;
+import org.thyy.scheduled.entity.mainbusiness.Inpatient;
+
+import java.util.Date;
+import java.util.List;
 
 @Mapper
 public interface MainBusinessDo {
@@ -10,4 +16,75 @@ public interface MainBusinessDo {
 
     @Update("update mzfz_zd_dept_room set patient_num=0 where patient_num!=0")
     void resetPatientNum();
+
+
+    @Select("select rtrim(inpatient_no) inpatient_no,admiss_times,admiss_date, " +
+            "ledgerSn=(select max(ledger_sn) from zy_ledger_file b where " +
+            "a.inpatient_no=b.inpatient_no and a.admiss_times=b.admiss_times) " +
+            "from zy_actpatient a ")
+    List<Inpatient> getInpatients();
+
+    @Update("update zy_detail_charge set charge_date=#{newDate} where inpatient_no=#{zyh} " +
+            "and admiss_times=#{times} and CONVERT(varchar(20), charge_date, 20) <= #{admdate}")
+    void correctFeeChargeTimeBeforeAdmiss(@Param("zyh") String zyh,
+                                          @Param("times") int times,
+                                          @Param("admdate") Date admdate,
+                                          @Param("newDate") Date newDate);
+
+
+    @Select("select start_time from yz_act_order where inpatient_no=#{patNo} and admiss_times=#{times} " +
+            "and status_flag > '1' and isnull(group_no, '00')='00' and order_code in ('06026','06053','05973')")
+    Date selectActOrderDisDate(@Param("patNo") String patNo, @Param("times") Integer times);
+
+    @Update("update zy_detail_charge set charge_date=#{disdate} where inpatient_no=#{zyh} " +
+            "and admiss_times=#{times} and charge_date>#{disdate}")
+    void correctFeeChargeTimeAfterDismiss(@Param("zyh") String zyh,
+                                          @Param("times") int times,
+                                          @Param("disdate") Date disdate);
+
+    @Update("update zy_ledger_file set deposit=(select isnull(sum(depo_amount),0) from zy_deposit_file f with(nolock) " +
+            "where f.inpatient_no=#{zyh} and f.admiss_times=#{times} and f.ledger_sn=#{ledger} and f.status in ('1','2') ) " +
+            "where inpatient_no=#{zyh} and admiss_times=#{times} and ledger_sn=#{ledger}")
+    void recountDeposit(@Param("zyh") String zyh,
+                        @Param("times") int times,
+                        @Param("ledger") int ledger);
+
+    @Update("execute zy_receive_drug #{zyh},#{times},#{zyh},#{infant} ")
+    void zyReceiveDrug(@Param("zyh") String zyh,
+                       @Param("times") int times,
+                       @Param("infant") int infant);
+
+    @Update("execute zy_receive_one #{zyh},#{times},#{zyh},#{infant} ")
+    void zyReceiveOne(@Param("zyh") String zyh,
+                      @Param("times") int times,
+                      @Param("infant") int infant);
+
+    @Update("execute dbo.zy_cngl_fyjs_list_yz #{zyh},#{times} ")
+    void zyCnglFyjsListYz(@Param("zyh") String zyh,
+                          @Param("times") int times);
+
+    @Update("update zy_detail_charge set charge_status='2' where " +
+            "inpatient_no=#{zyh} and admiss_times=#{times} and ledger_sn=#{ledger} ")
+    void updateZyDetailCharge(@Param("zyh") String zyh,
+                              @Param("times") int times,
+                              @Param("ledger") int ledger);
+
+    @Update("execute zy_calc_detail_again_new #{zyh},#{times},#{ledger},1,1")
+    void zyCalcDetailAgainNew(@Param("zyh") String zyh,
+                              @Param("times") int times,
+                              @Param("ledger") int ledger);
+
+    @Update("update zy_ledger_file set balance=(deposit+isnull(charge_yb,0)-total_charge) where " +
+            "inpatient_no=#{zyh} and admiss_times=#{times} and ledger_sn=#{ledger}")
+    void updateBalance(@Param("zyh") String zyh,
+                       @Param("times") int times,
+                       @Param("ledger") int ledger);
+
+    @Select("select balance from zy_ledger_file where inpatient_no=#{zyh} and admiss_times=#{times} and ledger_sn=#{ledger}")
+    String selectLedgerBalance(@Param("zyh") String zyh,
+                               @Param("times") int times,
+                               @Param("ledger") int ledger);
+
+    @Update("update zy_actpatient set balance=#{balance} where inpatient_no=#{zyh}")
+    void updateZyActPatientBalance(@Param("zyh") String zyh, @Param("balance") String balance);
 }

+ 105 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/entity/clockin/Clockin.java

@@ -0,0 +1,105 @@
+package org.thyy.scheduled.entity.clockin;
+
+import com.alibaba.fastjson2.JSONObject;
+import lombok.Data;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.Random;
+
+@Data
+public class Clockin {
+    private String starttime;
+    private String endtime;
+    private List<String> useridlist;
+
+    // 数据库表
+    private String userid;
+    private String groupname;
+    private String checkinType;
+    private String exceptionType;
+    private String checkinTime;
+    private String checkinDate;
+    private String checkinHour;
+    private String locationTitle;
+    private String locationDetail;
+    private String notes;
+
+    // flag, 判断是否信息科,默认否
+    private boolean isInformationDept = false;
+
+    // 需要从其他表获取的信息
+    private String name;
+    private String dept;
+
+    public void setTimes(String starttime, String endtime) {
+        this.starttime = starttime;
+        this.endtime = endtime;
+    }
+
+    public void setAllInfo(JSONObject item) {
+        setUserid(item.getString("userid"));
+        setGroupname(item.getString("groupname"));
+        setCheckinType(item.getString("checkin_type"));
+        setExceptionType(item.getString("exception_type"));
+        setCheckinTime(item.getString("checkin_time"));
+        setLocationTitle(item.getString("location_title"));
+        setLocationDetail(item.getString("location_detail"));
+        setNotes(item.getString("notes"));
+    }
+
+    public void setAllInfoForItDept(JSONObject item, String starttime) {
+        setUserid(item.getString("userid"));
+        if (getUserid().equals("2482") || getUserid().equals("2727")) {
+            setGroupname("自由打卡");
+        } else {
+            setGroupname("后勤白班打卡");
+        }
+        setCheckinType(item.getString("checkin_type"));
+        setStarttime(starttime);
+        setCheckinTime();
+        setExceptionType("");
+        setLocationTitle("湖南泰和医院");
+        setLocationDetail("湖南省长沙市开福区芙蓉北路529号");
+        setNotes("");
+    }
+
+    public void setCheckinTime() {
+        String suffix = mkSuffix(getCheckinType());
+        this.checkinTime = getStarttime() + " " + suffix;
+        setCheckinDate(getStarttime().replaceAll("-", "/"));
+        setCheckinHour(suffix);
+    }
+
+    private String mkSuffix(String type) {
+        if (type.equals("上班打卡")) {
+            int random = new Random().nextInt(19) + 40;
+            return "07:" + random;
+        } else {
+            int random = new Random().nextInt(49) + 10;
+            Calendar calendar = Calendar.getInstance();
+            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
+                return "12:" + random;
+            }
+            return random < 35 ? "18:" + random : "17:" + random;
+        }
+    }
+
+    public void setCheckinTime(String checkinTime) {
+        if (checkinTime.contains("-")) {
+            this.checkinTime = checkinTime;
+        } else {
+            long timeStamp = Long.parseLong(checkinTime) * 1000;
+            DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
+            this.checkinTime = df.format(new Date(timeStamp));
+            String[] datetime = this.checkinTime.split(" ");
+            setCheckinDate(datetime[0]);
+            setCheckinHour(datetime[1]);
+            if (getExceptionType().equals("未打卡"))
+                setCheckinHour("");
+        }
+    }
+}

+ 13 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/entity/mainbusiness/Inpatient.java

@@ -0,0 +1,13 @@
+package org.thyy.scheduled.entity.mainbusiness;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class Inpatient {
+    private String inpatientNo;
+    private Integer admissTimes;
+    private Date admissDate;
+    private Integer ledgerSn;
+}

+ 110 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/service/FetchClockinData.java

@@ -0,0 +1,110 @@
+package org.thyy.scheduled.service;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+import org.thyy.scheduled.config.constant.Corpwx;
+import org.thyy.scheduled.dao.ClockinDao;
+import org.thyy.scheduled.entity.clockin.Clockin;
+import org.thyy.scheduled.utils.AccessTokenUtil;
+import org.thyy.scheduled.utils.DateUtil;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+
+@Slf4j
+@Service
+public class FetchClockinData {
+    private final Corpwx corpwx;
+    private final ClockinDao dao;
+    private final String api;
+
+    @Autowired
+    public FetchClockinData(Corpwx corpwx, ClockinDao dao) {
+        this.corpwx = corpwx;
+        this.dao = dao;
+        this.api = corpwx.getApiUrl() + "/checkin/getcheckindata?access_token=";
+    }
+
+    public void start() {
+        if (StrUtil.isNotBlank(corpwx.getApiUrl())
+                && StrUtil.isNotBlank(corpwx.getClockinSecret())) {
+            final String yesterday = getYesterdayDate();
+            Clockin request = new Clockin();
+            request.setTimes(yesterday, yesterday);
+            List<String> codeRsList = dao.getAllEmployeeExceptItDept();
+            for (int i = 0; i < codeRsList.size(); i += 30) {
+                int index = Math.min(i + 30, codeRsList.size());
+                List<String> useridlist = codeRsList.subList(i, index);
+                request.setUseridlist(useridlist);
+                log.info("getting... " + yesterday + " - " + useridlist);
+                getSignInfo(request);
+            }
+            codeRsList.clear();
+            codeRsList = dao.getAllEmployeeInItDept();
+            request.setInformationDept(true);
+            request.setUseridlist(codeRsList);
+            log.info("getting... " + yesterday + " - " + codeRsList.toString());
+            getSignInfo(request);
+            log.info("over.");
+        }
+    }
+
+    private String getYesterdayDate() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.add(Calendar.DAY_OF_YEAR, -1);
+        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
+        return df.format(calendar.getTime());
+    }
+
+    private void getSignInfo(Clockin param) {
+        String requestUrl = api + AccessTokenUtil.getInstance().get("corpwxClockinToken");
+        JSONObject postStr = new JSONObject();
+        postStr.put("opencheckindatatype", 3);
+        postStr.put("starttime", DateUtil.getTimestamp(param.getStarttime() + " 00:01:00") / 1000);
+        postStr.put("endtime", DateUtil.getTimestamp(param.getEndtime() + " 23:59:00") / 1000);
+        postStr.put("useridlist", param.getUseridlist());
+
+        RestTemplate restTemplate = new RestTemplate();
+        String httpRes = restTemplate.postForObject(requestUrl, postStr, String.class);
+        JSONObject res = JSONObject.parseObject(httpRes);
+        assert res != null;
+        if (res.getIntValue("errcode") == 0) {
+            JSONArray data = res.getJSONArray("checkindata");
+            if (null != data && !data.isEmpty()) {
+                if (param.isInformationDept()) {
+                    analyzeItDeptData(data, param.getStarttime());
+                } else {
+                    analyzeElseDeptData(data);
+                }
+            }
+        }
+    }
+
+    private void analyzeElseDeptData(JSONArray data) {
+        List<Clockin> list = new ArrayList<>();
+        for (int i = 0; i < data.size(); i++) {
+            Clockin item = new Clockin();
+            item.setAllInfo(data.getJSONObject(i));
+            list.add(item);
+        }
+        dao.insertIntoTable(list);
+    }
+
+    private void analyzeItDeptData(JSONArray data, String starttime) {
+        List<Clockin> list = new ArrayList<>();
+        for (int i = 0; i < data.size(); i++) {
+            Clockin item = new Clockin();
+            item.setAllInfoForItDept(data.getJSONObject(i), starttime);
+            list.add(item);
+        }
+        dao.insertIntoTable(list);
+    }
+}

+ 57 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/service/ReceiveCost.java

@@ -0,0 +1,57 @@
+package org.thyy.scheduled.service;
+
+import cn.hutool.core.util.StrUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.thyy.scheduled.dao.MainBusinessDo;
+import org.thyy.scheduled.entity.mainbusiness.Inpatient;
+import org.thyy.scheduled.utils.DateUtil;
+
+import java.util.Date;
+import java.util.List;
+
+@Slf4j
+@Service
+public class ReceiveCost {
+    private final MainBusinessDo dao;
+
+    @Autowired
+    public ReceiveCost(MainBusinessDo dao) {
+        this.dao = dao;
+    }
+
+    public void start() {
+        List<Inpatient> inpatients = dao.getInpatients();
+        for (Inpatient item : inpatients) {
+            if (item.getLedgerSn() != null) {
+                receiveAndRecalculateCost(item);
+            }
+        }
+    }
+
+    public void receiveAndRecalculateCost(Inpatient p) {
+        String zyh = p.getInpatientNo();
+        int times = p.getAdmissTimes();
+        int infant = zyh.startsWith("$") ? 1 : 0;
+        int ledger = p.getLedgerSn();
+        dao.correctFeeChargeTimeBeforeAdmiss(zyh, times, p.getAdmissDate(), DateUtil.timePlusSecond(p.getAdmissDate(), 60));
+        Date disdate = dao.selectActOrderDisDate(zyh, times);
+        if (null != disdate) {
+            // 如果费用发生时间小于入院时间那么就加一分钟。
+            dao.correctFeeChargeTimeAfterDismiss(zyh, times, disdate);
+        }
+        dao.recountDeposit(zyh, times, ledger);
+        dao.zyReceiveDrug(zyh, times, infant);
+        dao.zyReceiveOne(zyh, times, infant);
+        dao.zyCnglFyjsListYz(zyh, times);
+        dao.updateZyDetailCharge(zyh, times, ledger);
+        dao.zyCalcDetailAgainNew(zyh, times, ledger);
+        dao.updateBalance(zyh, times, ledger);
+        String balance = dao.selectLedgerBalance(zyh, times, ledger);
+        if (StrUtil.isBlank(balance)) {
+            balance = "0";
+        }
+        dao.updateZyActPatientBalance(zyh, balance);
+    }
+}

+ 23 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/task/ClockinDataTask.java

@@ -0,0 +1,23 @@
+package org.thyy.scheduled.task;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import org.thyy.scheduled.service.FetchClockinData;
+
+@Slf4j
+@Component
+public class ClockinDataTask {
+    private final FetchClockinData fetchClockinData;
+
+    @Autowired
+    public ClockinDataTask(FetchClockinData fetchClockinData) {
+        this.fetchClockinData = fetchClockinData;
+    }
+
+    @Scheduled(cron = "0 0 8 ? * *")
+    public void fetchClockinData() {
+        fetchClockinData.start();
+    }
+}

+ 12 - 2
thyy-scheduled/src/main/java/org/thyy/scheduled/task/MainBusinessTask.java

@@ -7,17 +7,20 @@ import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
 import org.thyy.scheduled.config.constant.Thyy;
 import org.thyy.scheduled.dao.MainBusinessDo;
+import org.thyy.scheduled.service.ReceiveCost;
 
 @Component
 public class MainBusinessTask {
-    private final MainBusinessDo dao;
     private final Thyy thyy;
+    private final MainBusinessDo dao;
+    private final ReceiveCost receiveCost;
     private final RestTemplate restTemplate;
 
     @Autowired
-    public MainBusinessTask(MainBusinessDo dao, Thyy thyy, RestTemplate restTemplate) {
+    public MainBusinessTask(MainBusinessDo dao, Thyy thyy, ReceiveCost receiveCost, RestTemplate restTemplate) {
         this.dao = dao;
         this.thyy = thyy;
+        this.receiveCost = receiveCost;
         this.restTemplate = restTemplate;
     }
 
@@ -70,4 +73,11 @@ public class MainBusinessTask {
     public void resetPatientNum() {
         dao.resetPatientNum();
     }
+
+    @Scheduled(cron = "0 30 23 * * ?")
+    public void receiveCost() {
+        if (StrUtil.isNotBlank(thyy.getMainAddress())) {
+            receiveCost.start();
+        }
+    }
 }

+ 29 - 0
thyy-scheduled/src/main/java/org/thyy/scheduled/utils/DateUtil.java

@@ -0,0 +1,29 @@
+package org.thyy.scheduled.utils;
+
+import org.thyy.utils.exception.BizException;
+import org.thyy.utils.exception.ExceptionEnum;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+public class DateUtil {
+
+    public static Date timePlusSecond(Date date, int second) {
+        Calendar cal = Calendar.getInstance();
+        cal.setTime(date);
+        cal.add(Calendar.SECOND, second);
+        return cal.getTime();
+    }
+
+    public static long getTimestamp(String datetime) {
+        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        try {
+            return df.parse(datetime).getTime();
+        } catch (ParseException e) {
+            throw new BizException(ExceptionEnum.INTERNAL_SERVER_ERROR, e.getMessage());
+        }
+    }
+}