package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.TongyiCeliverableBondDataDao;
import com.friday.custody.entity.TongyiCeliverableBondDataEntity;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.exception.RRException;
import com.friday.custody.mapstruct.TongyiCeliverableBondMapper;
import com.friday.custody.req.ConvertibleBondsIssuedReq;
import com.friday.custody.service.basicService.TongyiCeliverableBondDataService;
import com.friday.custody.utils.PageUtil;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import com.friday.custody.utils.R;
import com.friday.custody.vo.TongyiCeliverableBondVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("tongyiCeliverableBondDataService")
public class TongyiCeliverableBondDataServiceImpl extends ServiceImpl<TongyiCeliverableBondDataDao, TongyiCeliverableBondDataEntity> implements TongyiCeliverableBondDataService {
@Autowired
    TongyiCeliverableBondDataService tongyiCeliverableBondDataService;

    /**
     * 更新可交债数据
     * @return
     */
    @Override
    public boolean updateTongyiCeliverableToTempTable() {
        //清空表
        this.remove(null);
        //查询数据
        List<TongyiCeliverableBondDataEntity> entities = baseMapper.queryDeliveraBlebondsIssued();
        //保存数据
        if (!this.saveBatch(entities)){
            log.error("定时任务保存通怡可交债失败custody_tongyi_celiverable_bond_data");
            return false;
        }
        return true;
    }

    /**
     * 查询可交债已发行
     *
     * @param req
     * @return
     */
    @Override
    public R<ArrayList<TongyiCeliverableBondVo>> queryDeliveraBlebondsIssued(ConvertibleBondsIssuedReq req){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            QueryWrapper<TongyiCeliverableBondDataEntity> queryWrapper = new QueryWrapper<>();
            Map<String, Object> pageParams = PageUtil.getPageParams(req);
            //封装查询参数
            if (StringUtils.isNotBlank(req.getConvertibleBondCode())) {
                queryWrapper.like("trading_code", req.getConvertibleBondCode());
            }
            if (StringUtils.isNotBlank(req.getConvertibleBondName())) {
                queryWrapper.like("secuabbr", req.getConvertibleBondName());
            }
            if (StringUtils.isNotBlank(req.getPositiveShareCode())) {
                queryWrapper.like("stkexchange_code", req.getPositiveShareCode());
            }
            if (StringUtils.isNotBlank(req.getPositiveShareName())) {
                queryWrapper.like("stksecuabbr", req.getPositiveShareName());
            }
            queryWrapper.orderByDesc("listingdate");
            IPage<TongyiCeliverableBondDataEntity> page = this.page(
                    new Query<TongyiCeliverableBondDataEntity>().getPage(pageParams),
                    queryWrapper
            );
            List<TongyiCeliverableBondDataEntity> entities = page.getRecords();
            ArrayList<TongyiCeliverableBondVo> vos = new ArrayList<>();
            for (TongyiCeliverableBondDataEntity entity : entities) {
                TongyiCeliverableBondVo vo = TongyiCeliverableBondMapper.INSTANCT.entityToVo(entity);
                vos.add(vo);
            }
            PageUtils pageUtils = new PageUtils(page);
            stopWatch.stop();
            return R.ok("可交债部分查询成功"+stopWatch.getLastTaskTimeMillis()+"ms")
                    .put("totalCount",pageUtils.getTotalCount())
                    .put("currPage",pageUtils.getCurrPage())
                    .put("totalPage",pageUtils.getTotalPage())
                    .put("pageSize",pageUtils.getPageSize())
                    .put("data", vos);
        }catch (RRException rrException) {
            return R.error(RetCodeEnum.OPERATION_ERROR.getCode(), rrException.getMsg());
        } catch (Exception e) {
            return R.error(RetCodeEnum.OPERATION_ERROR.getCode(), "查询数据失败!");
        }
    }
}
