/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.enums.WxEnum;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxLwsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxLwsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxLwsqMapper;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxLwsqService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 微信-漏水报障申请
 *
 * @author admin
 * @date Mon May 26 00:00:00 GMT+08:00 2025
 */
@Service("wxLwsqService")
public class WxLwsqServiceImpl extends ServiceImpl<WxLwsqMapper, WxLwsq> implements WxLwsqService {
    @Override
    public Page<WxLwsqVo> getWxLwsqPage(Page page, WxCommonDto dto){
        QueryWrapper<WxLwsq> queryWrapper = new QueryWrapper<>();
        Date starttime = dto.getStarttime();
        Date endtime = dto.getEndtime();

        String searchText = dto.getSearchText();
        String state = dto.getState();
        //关键字查询
        if (null != searchText) {
            queryWrapper.and(
                    wrapper -> wrapper.like("register_name", searchText)
                            .or().like("register_address", searchText)
                            .or().like("register_phone", searchText)
            );
        }
        //添加时间条件
        if (null != starttime && null != endtime) {
            queryWrapper.ge("register_date", starttime).lt("register_date", endtime);
        } else if (null != starttime) {
            queryWrapper.ge("register_date", starttime);
        } else if (null != endtime) {
            queryWrapper.le("register_date", endtime);
        }
        if (StringUtil.isNotEmpty(state)) {
            queryWrapper.eq("state", state);
        }
        queryWrapper.orderByDesc("register_date");

        Page infoPageList = this.page(page, queryWrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    public List<WxLwsqVo> viewConversion(List<WxLwsq> infoList) {
        if (CollectionUtils.isEmpty(infoList)) {
            return Collections.emptyList();
        }

        List<WxLwsqVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            WxLwsqVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info), WxLwsqVo.class);

            if (null != info.getState()) {
                baseInfo.setStateName(WxEnum.getCodeForValue(info.getState()));
            }
            infoVoList.add(baseInfo);
        });
        return infoVoList;
    }
}
