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.TongyiDirectionalConvertibleBondsDao;
import com.friday.custody.entity.TongyiDirectionalConvertibleBondsEntity;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.exception.RRException;
import com.friday.custody.mapstruct.TongyiDirectionalConvertibleBondsMapper;
import com.friday.custody.req.ConvertibleBondsIssuedReq;
import com.friday.custody.service.basicService.TongyiDirectionalConvertibleBondsService;

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 lombok.extern.slf4j.Slf4j;
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;

/**
 * 通怡定向可转债
 * @author 021790
 */
@Service
@Slf4j
public class TongyiDirectionalConvertibleBondsServiceImpl extends ServiceImpl<TongyiDirectionalConvertibleBondsDao,
        TongyiDirectionalConvertibleBondsEntity>
implements TongyiDirectionalConvertibleBondsService {



    @Override
    public boolean updateDirectionalConvertibleBondsToTempTable() {
        this.remove(null);
        List<TongyiDirectionalConvertibleBondsEntity> latestBonds = baseMapper.queryDirectionalConvertibleBonds();
            //save
        if (!this.saveBatch(latestBonds)){
                log.error("定时任务保存通怡定向可转债失败custody_tongyi_directional_convertible_bonds");
                return false;
        }
            return true;
    }
    @Override
    public R<ArrayList<TongyiConvertibleBondsVo>> queryDirectionalConvertibleBonds(ConvertibleBondsIssuedReq req){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            QueryWrapper<TongyiDirectionalConvertibleBondsEntity> 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<TongyiDirectionalConvertibleBondsEntity> page = this.page(
                    new Query<TongyiDirectionalConvertibleBondsEntity>().getPage(pageParams),
                    queryWrapper
            );
            List<TongyiDirectionalConvertibleBondsEntity> entities = page.getRecords();
            ArrayList<TongyiConvertibleBondsVo> vos = new ArrayList<>();
            for (TongyiDirectionalConvertibleBondsEntity entity : entities) {
                TongyiConvertibleBondsVo vo = TongyiDirectionalConvertibleBondsMapper.INSTANCT.entityToVo(entity);
                vos.add(vo);
            }
            PageUtils pageUtils = new PageUtils(page);
            stopWatch.stop();
            return R.ok("可转债已发行部分查询成功,"+stopWatch.getLastTaskTimeMillis())
                    .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(), "查询数据失败!");
        }

    }
}
