/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workreceiptssettle.service;

import java.math.BigDecimal;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.workinvoicedetail.entity.WorkInvoiceDetail;
import com.jeeplus.modules.workinvoicedetail.service.WorkInvoiceDetailService;
import com.jeeplus.modules.workreceiptssettle.dao.WorkReceiptsSettleDetailDao;
import com.jeeplus.modules.workreceiptssettle.entity.WorkReceiptsSettleDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;

/**
 * 收入结算Service
 * @author ssrh
 * @version 2018-06-28
 */
@Service
@Transactional(readOnly = true)
public class WorkReceiptsSettleDetailService extends CrudService<WorkReceiptsSettleDetailDao, WorkReceiptsSettleDetail> {

    @Autowired
    private WorkInvoiceDetailService invoiceDetailService;

	public WorkReceiptsSettleDetail get(String id) {
		return super.get(id);
	}
	
	public List<WorkReceiptsSettleDetail> findList(WorkReceiptsSettleDetail workReceiptsSettleDetail) {
        List<WorkReceiptsSettleDetail> settleDetailList = super.findList(workReceiptsSettleDetail);
        if (settleDetailList!=null){
            for (WorkReceiptsSettleDetail settleDetail : settleDetailList) {
                if(settleDetail.getInvoice()!=null&& StringUtils.isNotBlank(settleDetail.getInvoice().getId())) {
                    WorkInvoiceDetail workInvoiceDetail = invoiceDetailService.get(settleDetail.getInvoice().getId());
                    settleDetail.setInvoice(workInvoiceDetail);
                    if(workInvoiceDetail!=null&&StringUtils.isNotBlank(workInvoiceDetail.getIncomeMoney())&&StringUtils.isNotBlank(settleDetail.getSettlePct())){
                        BigDecimal incomeMoney = new BigDecimal(workInvoiceDetail.getIncomeMoney());
                        BigDecimal settlePct = new BigDecimal(settleDetail.getSettlePct().replace("%",""));
                        settleDetail.setSettleAmt(incomeMoney.multiply(settlePct).divide(new BigDecimal("100"),2,BigDecimal.ROUND_HALF_UP).doubleValue());
                    }
                }
            }
        }
        return settleDetailList;
    }
	
	public Page<WorkReceiptsSettleDetail> findPage(Page<WorkReceiptsSettleDetail> page, WorkReceiptsSettleDetail workReceiptsSettleDetail) {
		return super.findPage(page, workReceiptsSettleDetail);
	}
	
	@Transactional(readOnly = false)
	public void save(WorkReceiptsSettleDetail workReceiptsSettleDetail) {
		super.save(workReceiptsSettleDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(WorkReceiptsSettleDetail workReceiptsSettleDetail) {
		super.delete(workReceiptsSettleDetail);
	}


    public void deleteBySettleId(String settleId) {
        dao.deleteBySettleId(settleId);
    }

    public boolean checkPayStatus(String invoiceId) {
        WorkReceiptsSettleDetail workReceiptsSettleDetail = new WorkReceiptsSettleDetail();
        workReceiptsSettleDetail.setInvoice(new WorkInvoiceDetail(invoiceId));
        List<WorkReceiptsSettleDetail> list = dao.findList(workReceiptsSettleDetail);
	    if (list==null || list.size()<=0) return false;
        WorkReceiptsSettleDetail settleDetail = list.get(0);
        return "1".equals(settleDetail.getStatus());
    }

    @Transactional(readOnly = false)
    public void updatePayStatus(WorkReceiptsSettleDetail settleDetail) {
	    dao.updatePayStatus(settleDetail);
    }
}