/*
 * 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.bjgl.entity.YhCbkxx;
import com.tofly.feesapi.common.enums.WxEnum;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.common.util.HttpRequestUtil;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxDbsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxXhsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxDbsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxXhsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxXhsqService;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.entity.YhhLog;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxXhsqMapper;
import org.springframework.transaction.support.TransactionTemplate;
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("wxXhsqService")
public class WxXhsqServiceImpl extends ServiceImpl<WxXhsqMapper, WxXhsq> implements WxXhsqService {
    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 自定义分页查询
     *
     * @param page
     * @param dto
     * @return
     */
    @Override
    public Page<WxXhsqVo> getWxXhsqPage(Page page, WxCommonDto dto) {
        WxXhsq wxXhsq = JSONObject.parseObject(JSON.toJSONString(dto), WxXhsq.class);
        QueryWrapper<WxXhsq> 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(wxXhsq);
        queryWrapper.orderByDesc("register_date");

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

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

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

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

    /**
     * 更新微信销户申请
     * *
     * @param update
     * @return
     */
    @Override
    public boolean updateWxXhsq(WxXhsq update) {
        String id = update.getId();
        if (StringUtils.isEmpty(id)) throw new FeesException("id不能为空", FeesErrorCode.BUSINESS_ERROR);
        WxXhsq wxXhsq = this.getById(id);
        if (wxXhsq == null)
            throw new FeesException(String.format("id(%s)不存在", id), FeesErrorCode.BUSINESS_ERROR);
        BeanUtils.copyProperties(update, wxXhsq);



        //获取token
        SecurityUtils.getTokenValue();

        transactionTemplate.execute((status) -> {
            try {
                this.updateById(wxXhsq);
                //已处理
                if(wxXhsq.getState().equals("2")){
                    //往用户销户日志表添加数据
                    YhhLog yhhLog = new YhhLog();
                    yhhLog.setBh(GuidGenerator.getGuid().substring(0, 8));
                    yhhLog.setGdzt("1");//处理中
                    yhhLog.setCzrq(new Date());
                    yhhLog.setCzryxm(SecurityUtils.getUser().getRealName());
                    yhhLog.setCzrybm(SecurityUtils.getUserId().toString());
                    yhhLog.setWxid(wxXhsq.getId());
                    //yhhLog.setXhsj();

                }
            } catch (Exception e) {
                status.setRollbackOnly();
                throw e;
            }
            return 1;
        });
        return true;
    }
}
