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.TongyiUnissuedDataDao;
import com.friday.custody.entity.TongyiUnissuedDataEntity;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.exception.RRException;
import com.friday.custody.mapstruct.TongyiUnissuedMapper;
import com.friday.custody.req.MarketToBeIssuedReq;
import com.friday.custody.service.basicService.TongyiUnissuedDataService;
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.TongyiUnissuedDataVo;
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("tongyiUnissuedDataService")
public class TongyiUnissuedDataServiceImpl extends ServiceImpl<TongyiUnissuedDataDao, TongyiUnissuedDataEntity> implements TongyiUnissuedDataService {
@Autowired
    TongyiUnissuedDataService tongyiUnissuedDataService;

    /**
     * 更新全市场待发行项目
     * @return
     */
    @Override
   public boolean updateTongyiUnissuedToTempTable(){
        //清空表
        this.remove(null);
        //查询数据 全市场待发行项目：定向可转债/可转债/可交债
        List<TongyiUnissuedDataEntity> queryUnissuedsOne = baseMapper.queryUnissuedsOne();
        //查询数据 全市场待发行项目：定增
        //List<TongyiUnissuedDataEntity> queryUnissuedsTwo = baseMapper.queryUnissuedsTwo();
        //保存数据
        boolean saveBatchOne = this.saveBatch(queryUnissuedsOne);
        //boolean saveBatchTwo = this.saveBatch(queryUnissuedsTwo);
        if (!saveBatchOne /*|| !saveBatchTwo*/){
            log.error("定时任务保存通怡全市场待发行失败custody_tongyi_unissued_data");
            return false;
        }
        return true;
    }
    @Override
     public R<ArrayList<TongyiUnissuedDataVo>> queryMarketToBeIssued(MarketToBeIssuedReq req){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
             QueryWrapper<TongyiUnissuedDataEntity> queryWrapper = new QueryWrapper<>();
             Map<String, Object> pageParams = PageUtil.getPageParams(req);
             //封装查询参数
             if (StringUtils.isNotBlank(req.getProjectProgress())) {
                 queryWrapper.eq("listdate", req.getProjectProgress());
             }
             if (StringUtils.isNotBlank(req.getCode())) {
                 queryWrapper.like("tradingcode", req.getCode());
             }
             if (StringUtils.isNotBlank(req.getIndustry())) {
                 queryWrapper.eq("indunameiv", req.getIndustry());
             }
             if (StringUtils.isNotBlank(req.getBroker())) {
                 queryWrapper.like("leadunderwriter", req.getBroker());
             }
             if (StringUtils.isNotBlank(req.getRefinancingMode())) {
                 queryWrapper.eq("refinancingmode", req.getRefinancingMode());
             }
             IPage<TongyiUnissuedDataEntity> page = this.page(
                     new Query<TongyiUnissuedDataEntity>().getPage(pageParams),
                     queryWrapper
             );
             //封装数据
             PageUtils pageUtils = new PageUtils(page);
             List<TongyiUnissuedDataEntity> entities = page.getRecords();
             ArrayList<TongyiUnissuedDataVo> dtos = new ArrayList<>();
             for (TongyiUnissuedDataEntity entity : entities) {
                 TongyiUnissuedDataVo tongyiUnissuedDataVo = TongyiUnissuedMapper.INSTANCT.tongyiUnissuedData(entity);
                 dtos.add(tongyiUnissuedDataVo);
             }
             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", dtos);
         }catch (RRException rrException) {
            return R.error(RetCodeEnum.OPERATION_ERROR.getCode(), rrException.getMsg());
         } catch (Exception e) {
              return R.error(RetCodeEnum.OPERATION_ERROR.getCode(), "查询数据失败!");
         }
    }
}