package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.YpMzFytj;
import cn.hnthyy.thmz.service.his.mz.MzYpFydlService;
import cn.hnthyy.thmz.service.his.YpMzFytjService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Slf4j
public class YpfydlController {
    @Autowired
    private MzYpFydlService mzYpFydlService;
    @Autowired
    private YpMzFytjService ypMzFytjService;

    /**
     * 查询未发药药品
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getToMedicine", method = {RequestMethod.GET})
    public Map<String, Object> getToMedicine(@RequestParam("groupNo") String groupNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(groupNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询未发药药品失败,未选择药房");
                return resultMap;
            }
            Date now = new Date();
            Date begin = DateUtil.getFirstSecond(now);
            Date end = DateUtil.getLastSecond(now);
            YpMzFytj ypMzFytj = ypMzFytjService.querylastItem(groupNo, begin, end);
            //过号集合
            List<String> pastList = new ArrayList();
            //待取药集合
            List<String> currentList = new ArrayList();
            List<Map<String, Object>> all = mzYpFydlService.queryToMedicine(groupNo, begin, end);
            if (all != null && all.size() > 0) {
                for (Map<String, Object> map : all) {
                    Date priceTime = (Date) map.get("price_time");
                    String name = (String) map.get("name");
                    if (ypMzFytj != null && ypMzFytj.getChargeDate() != null) {
                        if (priceTime != null && priceTime.before(ypMzFytj.getChargeDate())) {
                            if (!pastList.contains(name)) {
                                pastList.add(name);
                            }
                            continue;
                        }
                    }
                    if (!currentList.contains(name)) {
                        currentList.add(name);
                    }
                }
            }
//            pastList = Arrays.asList("过号1","过号2","过号3","过号4","过号5","过号6","过号7","过号8");
            //          currentList = Arrays.asList("取药取药","取药2","取药3","取药4","取药5","取药6","取药7","取药8","取药9","取药10","取药11","取药12","取药13","取药14","取药15","取药16","取药17","取药18","取药19","取药20","取药21","取药22","取药23","取药24","取药25","取药26");
//            currentList = Arrays.asList("取药取药", "取药2", "取药3", "取药4");
            resultMap.put("code", 0);
            resultMap.put("message", "查询未发药药品成功");
            resultMap.put("pastList", pastList);
            resultMap.put("currentList", currentList);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询未发药药品失败,错误信息：【" + e.getMessage() + "】");
            log.error("查询未发药药品失败,错误信息：[]", e.getMessage());
            return resultMap;
        }
    }
}
