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.TongyiDirectionalupDataDao;
import com.friday.custody.entity.TongyiDirectionalupDataEntity;
import com.friday.custody.enums.RetCodeEnum;
import com.friday.custody.mapstruct.TongyiDirectionalupMapper;
import com.friday.custody.req.ConvertibleBondsIssuedReq;
import com.friday.custody.service.basicService.TongyiDirectionalupDataService;
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.ConvertibleBondsIssuedVo;
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("tongyiDirectionalupDataService")
public class TongyiDirectionalupDataServiceImpl extends ServiceImpl<TongyiDirectionalupDataDao, TongyiDirectionalupDataEntity> implements TongyiDirectionalupDataService {


    //更新【定增】部分
    @Override
    public boolean updateTongyiDirectionalupToTempTable() {
        //清空表
        this.remove(new QueryWrapper<>());
        //查询数据
        List<TongyiDirectionalupDataEntity> entities = baseMapper.queryTongyiDirectionalup();

        //保存数据
        if (!this.saveBatch(entities)){
            log.error("定时任务保存通怡定增失败custody_tongyi_directionalup_data");
            return false;
        }
        return true;
    }
    //查询【定增】部分
    @Override
    public R<List<ConvertibleBondsIssuedVo>> queryFixedIncreaseIssued(ConvertibleBondsIssuedReq req){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            QueryWrapper<TongyiDirectionalupDataEntity> 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.getCorporateName())) {
                queryWrapper.like("chiname", req.getCorporateName());
            }
            if (StringUtils.isNotBlank(req.getLeadUnderwriter())) {
                queryWrapper.like("leadunderwriter", req.getLeadUnderwriter());
            }
            queryWrapper.orderByDesc("change_date");
            IPage<TongyiDirectionalupDataEntity> page = this.page(
                    new Query<TongyiDirectionalupDataEntity>().getPage(pageParams),
                    queryWrapper
            );
            //封装数据
            PageUtils pageUtils = new PageUtils(page);
            List<TongyiDirectionalupDataEntity> entities = page.getRecords();
            List<ConvertibleBondsIssuedVo> vos=new ArrayList<>();
            for (TongyiDirectionalupDataEntity entity : entities) {
                ConvertibleBondsIssuedVo convertibleBondsIssuedVo = TongyiDirectionalupMapper.INSTANCT.tongyiDirectionalupData(entity);
                vos.add(convertibleBondsIssuedVo);
            }
            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 (Exception e) {
            log.error( "查询数据失败!");
            return R.error(RetCodeEnum.OPERATION_ERROR.getCode(), "查询数据失败!");
        }
    }
}