package com.jianshui.invoice.service.impl.yuande;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.jianshui.invoice.domain.dto.yuande.YuandeInvoiceFormDTO;
import com.jianshui.invoice.domain.yuande.CorpBody;
import com.jianshui.invoice.domain.yuande.Invoiceformitem;
import com.jianshui.invoice.mapper.yuande.InvoiceFormItemMapper;
import com.jianshui.invoice.mapper.yuande.InvoiceFormMapper;
import com.jianshui.invoice.service.yuande.IInvoiceFormService;
import com.jianshui.invoice.utils.yuande.EntityToMap;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
@Data
public class InvoiceFormServiceImpl implements IInvoiceFormService {
    @Autowired
    private InvoiceFormMapper invoiceFormMapper;
    @Autowired
    private InvoiceFormItemMapper invoiceFromItemMapper;

    /** 前一天日期 */
    private static String date1;

    /** 后一天日期 */
    private static String date2;

    /**
     * @Description 将所有信息封装一起
     **/
    @Override
    public JSONObject getPushInfo(CorpBody corpBody) {

        //判断参数是否为空
        if (ObjectUtils.isEmpty(corpBody)){
            return null;
        }
        //获取日期转换器
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //获取当天日期
        date2 = dateFormat.format(date);
        //查询传输信息
        List<YuandeInvoiceFormDTO> invoiceFormList = invoiceFormMapper.findInvoiceFormList(date1,date2);
        //date1转换为当天日期
        date1 = date2;
        //获取所有开票信息的id集合
        List<String> ids = invoiceFormList.stream().map(YuandeInvoiceFormDTO::getId).collect(Collectors.toList());
        //将开票信息放入objectData
        Map<String, Object> objectData = MapUtil.newHashMap();
        objectData.put("object_data",invoiceFormList);
        //查询开票信息明细
        List<Invoiceformitem> allPushInfo = invoiceFromItemMapper.findAllPushInfo(ids);
        //将开票信息明细放入到details
        Map<String, Object> details = MapUtil.newHashMap();
        details.put("Invoiceformitem__c",allPushInfo);
        //将objectData和details放入到data中
        Map<String, Object> data = MapUtil.newHashMap();
        data.put("object_data",objectData);
        data.put("details",details);
        //将信息放入到jsonobject中
        JSONObject dataMap = new JSONObject();
        dataMap.put("corpAccessToken",corpBody.getCorpAccessToken());
        dataMap.put("corpId",corpBody.getCorpId());
        dataMap.put("currentOpenUserId","FSUID_5DCF10DE85AA29CF6ECF414C2F22F669");
        dataMap.put("data",data);
        //返回结果集
        return dataMap;
    }

    /**
     * @Description 一条开票信息分装
     **/
    @Override
    public List<JSONObject>  getPushInfos(CorpBody corpBody) {

        //判断参数是否为空
        if (ObjectUtils.isEmpty(corpBody)){
            log.info("传入应用授权参数为空");
            return null;
        }
        //获取日期转换器
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //获取当天日期
        date2 = dateFormat.format(date);
        //查询开票信息
        List<YuandeInvoiceFormDTO> invoiceFormList = invoiceFormMapper.findInvoiceFormListWithInvoiceFromItem(date1,date2);
        //date1转换为当天日期
        date1 = date2;
        if (Objects.isNull(invoiceFormList)){
            log.info("查询到的开票信息表为空");
            return null;
        }
        //创建一个list封装返回值
        List<JSONObject> list = new ArrayList<>();
        for (YuandeInvoiceFormDTO invoiceFromDTO : invoiceFormList) {

            //details信息封装
            HashMap<String, Object> details = MapUtil.newHashMap();
            details.put("Invoiceformitem__c",invoiceFromDTO.getInvoiceformitems());

            //objectData封装
            Map<String, Object> objectData = EntityToMap.toMap(invoiceFromDTO);
            objectData.remove("invoiceformitems");
            //data数据封装
            HashMap<String, Object> data = MapUtil.newHashMap();
            data.put("object_data",objectData);
            data.put("details",details);
            //创建返回json
            JSONObject dataMap = new JSONObject();
            //添加认证信息
            dataMap.put("corpAccessToken",corpBody.getCorpAccessToken());
            dataMap.put("corpId",corpBody.getCorpId());
            dataMap.put("currentOpenUserId","FSUID_5DCF10DE85AA29CF6ECF414C2F22F669");
            dataMap.put("data",data);

            //将数据添加到集合
            list.add(dataMap);
        }

        return list;
    }

    /**
     * @Description 分页进行封装
     **/
    @Override
    public List<JSONObject> getPushInfo(CorpBody corpBody, int pageSize) {

        //判断参数是否为空
        if (ObjectUtils.isEmpty(corpBody)){
            log.info("传入应用授权参数为空");
            return null;
        }
        //获取日期转换器
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //获取当天日期
        date2 = dateFormat.format(date);
        //查询信息总数
        long total = invoiceFormMapper.findTotal(date1, date2);
        //计算总页数
        int pageTotal = (int) (total % pageSize == 0 ? total / pageSize:total / pageSize+1);
        //当前页数
        int currentPage = 1;
        //创建集合
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < pageTotal; i++) {
            //查询传输信息
            List<YuandeInvoiceFormDTO> invoiceFormList = invoiceFormMapper.findInvoiceFormByPage(date1,date2,pageSize,(currentPage-1)*pageSize);
            //判断查询信息是否为空
            if (ObjectUtils.isEmpty(invoiceFormList)) {
                log.info("查询到开票信息为空");
                return null;
            }
            //获取开票信息id集合
            List<String> ids = invoiceFormList.stream().map(YuandeInvoiceFormDTO::getId).collect(Collectors.toList());
            //将开票信息放入objectData
            Map<String, Object> objectData = MapUtil.newHashMap();
            objectData.put("object_data",invoiceFormList);
            //查询开票信息明细
            List<Invoiceformitem> invoiceformitems = invoiceFromItemMapper.findAllPushInfo(ids);
            //将开票信息明细放入到details
            Map<String, Object> details = MapUtil.newHashMap();
            details.put("Invoiceformitem__c",invoiceformitems);
            //将objectData和details放入到data中
            Map<String, Object> data = MapUtil.newHashMap();
            data.put("object_data",objectData);
            data.put("details",details);
            //将信息放入到jsonobject中
            JSONObject dataMap = new JSONObject();
            dataMap.put("corpAccessToken",corpBody.getCorpAccessToken());
            dataMap.put("corpId",corpBody.getCorpId());
            dataMap.put("currentOpenUserId","FSUID_5DCF10DE85AA29CF6ECF414C2F22F669");
            dataMap.put("data",data);
            list.add(dataMap);
            //当前页进入下一页
            currentPage++;
        }
        //date1转换为当天日期
        date1 = date2;
        return list;
    }
}
