package cn.hnthyy.thmz.controller.zy;

import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.service.his.zy.ZyDailySettlementService;
import cn.hnthyy.thmz.service.his.zy.ZyDayclosingRecordService;
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 javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class ZyDailySettlementController {
    @Autowired
    private ZyDailySettlementService zyDailySettlementService;
    @Autowired
    private ZyDayclosingRecordService zyDayclosingRecordService;
    /**
     * 获取未结算日结报表的明细
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getZyDailySettlementVo", method = {RequestMethod.GET})
    public Map<String, Object> getZyDailySettlementVo(HttpServletRequest httpServletRequest) {
        log.info("获取未结算日结报表的明细");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "操作成功");
            resultMap.put("user", tokenUser);
            resultMap.put("data", zyDailySettlementService.getZyDailySettlementVo(tokenUser.getUserIdCode()));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "获取未结算日结报表的明细出错，请联系管理员");
            log.error("获取未结算日结报表的明细异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }

    /**
     * 住院报表日结
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/dayclosing", method = {RequestMethod.GET})
    public Map<String, Object> dayclosing(@RequestParam("dateEnd")String dateEnd, HttpServletRequest httpServletRequest) {
        log.info("住院报表日结");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(StringUtils.isBlank(dateEnd)){
                resultMap.put("code", -1);
                resultMap.put("message", "结算时间为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            zyDayclosingRecordService.dayclosing(DateUtil.pase(dateEnd,"yyyy-MM-dd HH:mm:ss"),tokenUser.getUserIdCode());
            resultMap.put("code", 0);
            resultMap.put("message", "住院报表日结成功");
            resultMap.put("user", tokenUser);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "住院报表日结出错，请联系管理员");
            log.error("住院报表日结异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }


}
