package com.jpsoft.shinestar.modules.base.service.impl;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.CompanyPaymentInfoDAO;
import com.jpsoft.shinestar.modules.base.entity.CompanyPaymentInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyPaymentInfoService;
import com.jpsoft.shinestar.modules.business.entity.BillPush;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 墨鱼_mo
 * @date 2020-4-14 11:19
 */
@Transactional
@Component(value="CompanyPaymentInfoService")
@Slf4j
public class CompanyPaymentInfoServiceImpl implements CompanyPaymentInfoService {

    @Resource(name="companyPaymentInfoDAO")
    private CompanyPaymentInfoDAO companyPaymentInfoDAO;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public CompanyPaymentInfo get(Long id) {
        return companyPaymentInfoDAO.get(id);
    }

    @Override
    public CompanyPaymentInfo findByCompanyId(String companyId) {
        return companyPaymentInfoDAO.findByCompanyId(companyId);
    }

    @Override
    public List<CompanyPaymentInfo> findListByCompanyId(String companyId){
        return companyPaymentInfoDAO.findListByCompanyId(companyId);
    }

    @Override
    public List<CompanyPaymentInfo> list(){
        return  companyPaymentInfoDAO.list();
    }

    @Override
    public List<CompanyPaymentInfo> findByFinancePersonId(Long personId) {
        return companyPaymentInfoDAO.findByFinancePersonId(personId);
    }

    @Override
    public void createDelayedNotPayBill(BillPush billPush) {
        try{

            CompanyPaymentInfo companyPaymentInfo = get(Long.valueOf(billPush.getCollectionAccountId()));

            Date endDate = DateUtil.offsetDay(new Date(),Integer.valueOf(companyPaymentInfo.getCycle()));

            long millisecond = DateUtil.betweenMs(DateUtil.date(),endDate);

            log.warn("创建未支付账单发送消息的延时消息，id："+billPush.getId());
            rabbitTemplate.convertAndSend("billPushDelayExchange", "billPushSubmit",
                    billPush.getId(), (message)->{
                        message.getMessageProperties().setHeader("x-delay",millisecond);
                        return message;
                    });

        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }


    @Override
    public int insert(CompanyPaymentInfo companyPaymentInfo){
        return companyPaymentInfoDAO.insert(companyPaymentInfo);
    }

    @Override
    public int update(CompanyPaymentInfo companyPaymentInfo){
        return companyPaymentInfoDAO.update(companyPaymentInfo);
    }


    @Override
    public Page<CompanyPaymentInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize, boolean count, List<Sort> sortList) {
        Page<CompanyPaymentInfo> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            companyPaymentInfoDAO.search(searchParams,sortList);
        });

        return page;
    }
}
