package com.dxhy.core.controller.mailGather;

import cn.hutool.core.date.DateUtil;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.R;
import com.dxhy.core.service.mailGather.MailGatherLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * (MailGatherLog)表控制层
 *
 * @author makejava
 * @since 2023-03-23 08:42:35
 */
@Slf4j
@RestController
@RequestMapping("/mailGatherLog")
public class MailGatherLogController extends AbstractController {
    /**
     * 服务对象
     */
    @Resource
    private MailGatherLogService mailGatherLogService;
    final SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 分页查询
     *
     * @param pramsMap 筛选条件
     * @return 查询结果
     */
    @PostMapping("/queryByPage")
    public ResponseEntity<R> queryByPage(@RequestBody Map<String, Object> pramsMap) {

//        pramsMap.put("dbName", getUserInfo().getDbName());
//        if (pramsMap.get("username") == null || "".equals(pramsMap.get("username"))) {
//            return ResponseEntity.ok(R.error("收件人姓名不能为空！"));
//        }
        if (pramsMap.get("parseStart") == null || "".equals(pramsMap.get("parseStart"))) {
            return ResponseEntity.ok(R.error("开始日期不能为空！"));
        }
        if (pramsMap.get("parseEnd") == null || "".equals(pramsMap.get("parseEnd"))) {
            return ResponseEntity.ok(R.error("结束日期不能为空！"));
        }
        Object parseStart = pramsMap.get("parseStart");
        Object parseEnd = pramsMap.get("parseEnd");
        if (parseEnd == null || parseEnd == null) {
//            return ResponseEntity.ok(R.error("开始时间不能大于结束时间"));
        } else {
            try {
                Date starttime = sf2.parse(String.valueOf(parseStart));
                Date endtime = sf2.parse(String.valueOf(parseEnd));

                Date parse = DateUtil.beginOfDay(starttime);
                String startTime = DateUtil.format(parse, "yyyy-MM-dd HH:mm:ss");
                pramsMap.put("startTime", startTime);

                Date parse1 = DateUtil.endOfDay(endtime);
                String endTime = DateUtil.format(parse1, "yyyy-MM-dd HH:mm:ss");
                pramsMap.put("endTime", endTime);

//                if (starttime.after(endtime)) {
//                    return ResponseEntity.ok(R.error("开始时间不能大于结束时间"));
//                }
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        if(pramsMap.get("deptId") != null && "99".equals(pramsMap.get("deptId"))){
            pramsMap.put("deptId","");
        }

        String loginname = getUserInfo().getLoginname();
        pramsMap.put("createUser",loginname);
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return ResponseEntity.ok(R.ok().put("data",this.mailGatherLogService.queryByPage(pramsMap)));
    }

}

