package com.ruoyi.web.v3.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.v3.entity.HbRiskCheckDailyEntity;
import com.ruoyi.web.v3.entity.HbRiskCheckMonthlyEntity;
import com.ruoyi.web.v3.mapper.HbRiskCheckMonthlyMapper;
import com.ruoyi.web.v3.model.request.HbRiskCheckMonthlyPageReq;
import com.ruoyi.web.v3.service.IHbRiskCheckMonthlyService;
import com.ruoyi.web.v3.utils.QueryWrapperBuildUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * v3.0 隐患风险点检记录（月次） 服务实现类
 * </p>
 *
 */
@Service
public class HbRiskCheckMonthlyServiceImpl extends ServiceImpl<HbRiskCheckMonthlyMapper, HbRiskCheckMonthlyEntity> implements IHbRiskCheckMonthlyService {

    @Transactional
    @Override
    public AjaxResult add(HbRiskCheckMonthlyEntity request) {
        //入参检测
        if(ObjectUtils.isEmpty(request.getIsChecked())) {
            return error("请输入【排查结果】！");
        }
        //防重检测，
//        Integer count = lambdaQuery().eq(HbRiskCheckMonthlyEntity::getCheckDate, request.getCheckDate()).count();
//        if (count != 0) {
//            return error("请勿重复添加！");
//        }
        LocalDate checkDate = request.getCheckDate().toLocalDate();
        Integer countNum = lambdaQuery()
                .ge(HbRiskCheckMonthlyEntity::getCheckDate, checkDate.atStartOfDay())
                .lt(HbRiskCheckMonthlyEntity::getCheckDate, checkDate.plusDays(1).atStartOfDay())
                .count();
        request.setRecordNo(checkDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")) + countNum);
        return success("添加成功", baseMapper.insert(request));
    }

    @Transactional
    @Override
    public AjaxResult edit(HbRiskCheckMonthlyEntity request) {
        //数据格式校验
        if(ObjectUtils.isEmpty(request.getId())){
            return error("待更新数据未传入有效ID，请设置数据的唯一标识ID！");
        }
        //根据数据的id更新所有不为空的字段
        return updateById(request) ? success("修改成功！") : error("修改失败，请重试！");
    }

    @Transactional
    @Override
    public AjaxResult delete(String ids) {
        String[] idList = ids.split(",");
        return success("删除成功", removeByIds(Arrays.asList(idList)));
    }

    @Override
    public AjaxResult page(HbRiskCheckMonthlyPageReq request) {
        //将请求报文转为实体类
        HbRiskCheckMonthlyEntity entity = BeanUtil.toBean(request, HbRiskCheckMonthlyEntity.class);
        //构造查询条件（检索条件未定时，先按不为空的字段进行等值查询）
        QueryWrapper<HbRiskCheckMonthlyEntity> wrapper = QueryWrapperBuildUtil.notNoneEqWrapperHelper(entity);
        if(!ObjectUtils.isEmpty(request.getStartTime()) && !ObjectUtils.isEmpty(request.getEndTime())) {
            wrapper.lambda().between(HbRiskCheckMonthlyEntity::getCheckDate, request.getStartTime(), request.getEndTime());
        }
        //数据按id降序排列
        wrapper.lambda().orderByDesc(HbRiskCheckMonthlyEntity::getId);
        return success(baseMapper.selectPage(request.toPage(), wrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(lambdaQuery().orderByDesc(HbRiskCheckMonthlyEntity::getId).list());
    }
}
