/*
 * 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.WxLxdhsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxLxdhsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxLxdhsqService;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxLxdhsqMapper;
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("wxLxdhsqService")
public class WxLxdhsqServiceImpl extends ServiceImpl<WxLxdhsqMapper, WxLxdhsq> implements WxLxdhsqService {
    /**
     * 自定义分页查询
     *
     * @param page
     * @param dto
     * @return
     */
    @Override
    public Page<WxLxdhsqVo> getWxLxdhsqPage(Page page, WxCommonDto dto) {
        WxLxdhsq wxLxdhsq = JSONObject.parseObject(JSON.toJSONString(dto), WxLxdhsq.class);
        QueryWrapper<WxLxdhsq> 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(wxLxdhsq);
        queryWrapper.orderByDesc("register_date");

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

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

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

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