package com.huimv.receive.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.DataUill;
import com.huimv.receive.common.utils.Print;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BillIsolate;
import com.huimv.receive.entity.vo.IsolateVo;
import com.huimv.receive.mapper.BillIsolateMapper;
import com.huimv.receive.service.IBillIsolateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 * 隔离 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@Service
public class BillIsolateServiceImpl extends ServiceImpl<BillIsolateMapper, BillIsolate> implements IBillIsolateService {

    @Resource
    private BillIsolateMapper billIsolateMapper;

    @Override
    public Result listPersonalIsolate(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Page<BillIsolate> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<BillIsolate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admission_user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest)).orderByDesc("isolate_start_date");
        return new Result(ResultCode.SUCCESS, billIsolateMapper.selectPage(page, new QueryWrapper<BillIsolate>().eq("admission_user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest)).orderByDesc("sub_date")), billIsolateMapper.selectCount(queryWrapper));
    }

    @Override
    public Result Isolate(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");//0全部
        String farmId = paramsMap.get("farmId");
        if ("".equals(type) || null == type) {
            type = "0";
        }
        String dateType = paramsMap.get("dateType");
        if ("".equals(dateType) || null == dateType) {
            dateType = "3";
        }
        List<IsolateVo> isolateVos = null;
        QueryWrapper<BillIsolate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id",farmId);
        if (!"0".equals(type)) {
            queryWrapper.eq("isolate_location_id", type);
        }
        //今日
        if ("1".equals(dateType)) {
            Date timesmorning = DataUill.getTimesmorning();
            queryWrapper.ge("sub_date", timesmorning);

        } else if ("2".equals(type)) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("sub_date", calendar.getTime());

        } else if ("3".equals(type)) {
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("sub_date", timesMonthmorning);
        }
        isolateVos = billIsolateMapper.Isolate(queryWrapper);
        return new Result(ResultCode.SUCCESS, isolateVos);
    }

    @Override
    public Result IsolateSelect(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");//隔离状态
        String locationId = paramsMap.get("locationId");//隔离地点
        String word = paramsMap.get("word");
        String startTime = paramsMap.get("startTime");
        String endTime = paramsMap.get("endTime");
        String farmId = paramsMap.get("farmId");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        QueryWrapper<BillIsolate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(startTime)) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            queryWrapper.between("sub_date", startTime, endTime);
        }
        queryWrapper.eq("farm_id", farmId);
        if (StringUtils.isNotBlank(word)){
            queryWrapper.and(i -> i.like("phone", word)
                    .or().like( "admission_user_name", word));
        }
        queryWrapper.lambda().orderByDesc(BillIsolate::getId);
        queryWrapper.eq(StringUtils.isNotBlank(locationId), "isolate_location_id", locationId);
        queryWrapper.eq(StringUtils.isNotBlank(type), "bill_status", type);
        Page<BillIsolate> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS, billIsolateMapper.selectPage(page, queryWrapper));
    }

    @Override
    public void printIsolate(HttpServletResponse httpServletRequest, Map<String, String> paramsMap) throws Exception {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        List<BillIsolate> list = new ArrayList<>();
        for (String s : split) {
            BillIsolate admission = billIsolateMapper.selectById(s);
            list.add(admission);
        }
        Print.printIsolate(list);
    }
}
