package thyyxxk.webserver.service.reports;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.Capacity;
import thyyxxk.webserver.dao.his.reports.ClockinDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.clockin.Clockin;
import thyyxxk.webserver.entity.clockin.ClockinView;
import thyyxxk.webserver.entity.clockin.QueryFromDbParam;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ExcelUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author dj
 */
@Slf4j
@Service
public class ClockinService {
    private final ClockinDao dao;

    @Autowired
    public ClockinService(ClockinDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> getClockinDataFromDb(QueryFromDbParam param) {
        Map<String, Object> map = new HashMap<>(Capacity.TWO);
        IPage<Clockin> iPage = getClockInPage(param);
        map.put("totalSize", iPage.getTotal());
        map.put("list", combineData(iPage.getRecords()));
        return ResultVoUtil.success(map);
    }

    public void exportExcel(HttpServletResponse response, QueryFromDbParam param) {
        log.info("导出打卡数据Excel: {}", JSON.toJSONString(param));
        IPage<Clockin> iPage = getClockInPage(param);
        List<ClockinView> list = combineData(iPage.getRecords());
        String[] title = {"日期", "姓名", "工号", "科室", "打卡次数", "工作时长", "上班打卡时间", "上班打卡地点",
                "上班打卡状态", "上班打卡备注", "下班打卡时间", "下班打卡地点", "下班打卡状态", "下班打卡备注"};
        String[][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            ClockinView pojo = list.get(i);
            content[i][0] = pojo.getCheckinDate();
            content[i][1] = pojo.getName();
            content[i][2] = pojo.getUserid();
            content[i][3] = pojo.getDept();
            content[i][4] = String.valueOf(pojo.getTimes());
            content[i][5] = pojo.getWorktime();
            content[i][6] = pojo.getCheckinHourOn();
            content[i][7] = pojo.getCheckinAddrOn();
            content[i][8] = pojo.getCheckinStateOn();
            content[i][9] = pojo.getNotesOn();
            content[i][10] = pojo.getCheckinHourOff();
            content[i][11] = pojo.getCheckinAddrOff();
            content[i][12] = pojo.getCheckinStateOff();
            content[i][13] = pojo.getNotesOff();
        }
        ExcelUtil.exportExcel(response, title, content);
    }

    private IPage<Clockin> getClockInPage(QueryFromDbParam param) {
        IPage<Clockin> iPage = new Page<>(param.getCurrentPage(), param.getPageSize());
        // 如果当前时间小于选的结束时间的话就用
        if (DateUtil.endOfDay(new Date()).compareTo(param.getEnd()) < 0) {
            param.setEnd(DateUtil.endOfDay(new Date()));
        }
        return dao.getAllClockInDataFromDb(iPage, param.getStart(), param.getEnd());
    }

    private List<ClockinView> combineData(List<Clockin> origin) {
        HashMap<String, ClockinView> temp = new HashMap<>(Capacity.DEFAULT);
        for (Clockin item : origin) {
            String key = item.getUserid() + item.getCheckinDate();
            if (!temp.containsKey(key)) {
                ClockinView model = new ClockinView(item);
                model.setInfo(item);
                temp.put(key, model);
            } else {
                ClockinView model = temp.get(key);
                if (model.getTimes() == 2) {
                    continue;
                }
                model.setInfo(item);
                temp.replace(key, model);
            }
        }
        List<ClockinView> ret = new ArrayList<>();
        for (Map.Entry<String, ClockinView> entry : temp.entrySet()) {
            ret.add(entry.getValue());
        }
        return ret;
    }

}
