/*
 * 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.feesapi.common.enums.WxEnum;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxBtsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxBtsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxBtsqService;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxBtsqMapper;
import org.springframework.util.CollectionUtils;

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

/**
 * 微信-报停申请
 *
 * @author sunjian
 * @date Thu Feb 20 00:00:00 CST 2025
 */
@Service("wxBtsqService")
public class WxBtsqServiceImpl extends ServiceImpl<WxBtsqMapper, WxBtsq> implements WxBtsqService {
    /**
     * 自定义分页查询
     *
     * @param page
     * @param dto
     * @return
     */
    @Override
    public Page<WxBtsqVo> getWxBtsqPage(Page page, WxCommonDto dto) {
        WxBtsq wxBtsq = JSONObject.parseObject(JSON.toJSONString(dto), WxBtsq.class);
        QueryWrapper<WxBtsq> queryWrapper = new QueryWrapper<>();
        Date starttime = dto.getStarttime();
        Date endtime = dto.getEndtime();

        String searchText = dto.getSearchText();

        //关键字查询
        if (null != searchText) {
            queryWrapper.or().like("register_yhbh", searchText);
            queryWrapper.or().like("register_name", searchText);
            queryWrapper.or().like("register_address", searchText);
            queryWrapper.or().like("register_phone", searchText);
            dto.setSearchText(null);
        }
        //添加时间条件
        if (null != starttime && null != endtime) {
            queryWrapper.ge("register_date", starttime).lt("register_date", endtime);
            dto.setStarttime(null);
            dto.setEndtime(null);
        } else if (null != starttime) {
            queryWrapper.ge("register_date", starttime);
            dto.setStarttime(null);
        } else if (null != endtime) {
            queryWrapper.le("register_date", endtime);
            dto.setEndtime(null);
        }

        queryWrapper.setEntity(wxBtsq);
        queryWrapper.orderByDesc("register_date");

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

    /**
     * 转换视图
     *
     * @param infoList
     * @return
     */
    public List<WxBtsqVo> viewConversion(List<WxBtsq> infoList) {
        if (CollectionUtils.isEmpty(infoList)) {
            return Collections.emptyList();
        }

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

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