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.TongyiConvertibleBondsDataDao;
import com.friday.custody.entity.TongyiConvertibleBondsDataEntity;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.exception.RRException;
import com.friday.custody.mapstruct.TongyiConvertibleBondsMapper;
import com.friday.custody.req.ConvertibleBondsIssuedReq;
import com.friday.custody.service.basicService.TongyiConvertibleBondsDataService;
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.TongyiConvertibleBondsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

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


@Service("tongyiConvertibleBondsDataService")
public class TongyiConvertibleBondsDataServiceImpl extends ServiceImpl<TongyiConvertibleBondsDataDao, TongyiConvertibleBondsDataEntity>
        implements TongyiConvertibleBondsDataService {
    @Override
    public boolean updateTongyiConvertibleBondsToTempTable(){
        //清空表格
        this.remove(null);
        //查询数据
        ArrayList<TongyiConvertibleBondsDataEntity> entities = baseMapper.queryIssuedConvertibleBonds();
        if (!this.saveBatch(entities)) {
            log.error("定时任务保存通怡可转债已发行失败custody_tongyi_convertible_bonds_data");
            return false;
        }
        return true;
    }
    public void test(){
        QueryWrapper<TongyiConvertibleBondsDataEntity> queryWrapper = new QueryWrapper<>();
        List<TongyiConvertibleBondsDataEntity> entities = baseMapper.selectList(queryWrapper);
        System.out.println("====");
    }
    //查询可转债已发行
    @Override
    public R<ArrayList<TongyiConvertibleBondsVo>> queryIssuedConvertibleBonds(ConvertibleBondsIssuedReq req){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            QueryWrapper<TongyiConvertibleBondsDataEntity> 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.getLeadUnderwriter())) {
                queryWrapper.like("luname", req.getLeadUnderwriter());
            }
            if (StringUtils.isNotBlank(req.getPositiveShareCode())) {
                queryWrapper.like("stkexchange_code", req.getPositiveShareCode());
            }
            if (StringUtils.isNotBlank(req.getPositiveShareName())) {
                queryWrapper.like("stkexchange_secuabbr", req.getPositiveShareName());
            }
            queryWrapper.orderByDesc("listing_date");
            IPage<TongyiConvertibleBondsDataEntity> page = this.page(
                    new Query<TongyiConvertibleBondsDataEntity>().getPage(pageParams),
                    queryWrapper
            );
            List<TongyiConvertibleBondsDataEntity> entities = page.getRecords();
            ArrayList<TongyiConvertibleBondsVo> vos = new ArrayList<>();
            for (TongyiConvertibleBondsDataEntity entity : entities) {
                TongyiConvertibleBondsVo vo = TongyiConvertibleBondsMapper.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(), "查询数据失败!");
        }

    }
}