package com.ag.account.dao;

import com.ag.base.IBaseRepository;
import com.ag.entity.account.vo.BizVO;
import com.ag.entity.fee.*;
import com.ag.entity.fee.vo.ContractItemVO;
import com.ag.entity.fee.vo.ContractVO;
import com.ag.entity.fee.vo.TariffVO;
import com.ag.util.StrUtil;
import com.ag.util.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

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

/**
 * Created by Administrator on 2017/5/16 0016.
 */
@Repository
public class BizContractsDao {

    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<String> strRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<Tariff> tariffRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<TariffVO> tariffVORepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<TariffRitem> ritemRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ContractItem> contrItemRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ContractVO> contractVORepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<Contract> contractRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ContractItemVO> contrItemVORepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<TariffMultistep> stepORepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ItemProperties> itemPropertiesRepo;


    /**
     * 查询默认合同号
     */
    public String findDefaultContractId(BizVO bizVO) {
        String sql = "select ID from C_CONTRACT where IS_DEFAULT=1 and TENANCY_ID=?";
        if (bizVO.getBizTime() == null){
            bizVO.setBizTime(new Date());
        }
        sql = StrUtil.join(sql, " and EFFECTIVE_DATE<=", TimeUtil.toOrclDate(bizVO.getBizTime()));
        sql = StrUtil.join(sql, " and EXPIRED_DATE>=", TimeUtil.toOrclDate(bizVO.getBizTime()));
        List<String> lst = strRepo.listSingleColumn(sql, String.class, bizVO.getTenancyId());
        if(CollectionUtils.isEmpty(lst)){
            return null;
        }else{
            return lst.get(0);
        }
    }

    public ContractVO findContractVOById(String defaultContractId, BizVO billVO) {
        Contract contract = new Contract();
        contract.setId(defaultContractId);
        contract.setTenancyId(billVO.getTenancyId());
        String sql = contractRepo.getSelectByEntity(contract, false);
        return contractVORepo.get(sql, ContractVO.class);
    }

    /**
     * 合同号 + 费目编号 = 查找所有合同费目
     * @param defaultContractId
     * @param billVO
     * @return
     */
    public List<ContractItemVO> findItemByContract(String defaultContractId, BizVO billVO) {
        ContractItem ci = new ContractItem();
        ci.setTenancyId(billVO.getTenancyId());
        ci.setContractId(defaultContractId);
        ci.setLifeStatus(1);
        ci.setDelFlag(0);
        String sql = contrItemRepo.getSelectByEntity(ci, false);
        sql = StrUtil.join(sql, " and ITEM_ID in (", billVO.getItemIds(),")");
        //System.out.println(sql);
        return contrItemVORepo.list(sql, ContractItemVO.class);
    }

    /**
     * 合同号 + 费目编号 = 查找所有费率
     * @param defaultContractId
     * @param billVO
     * @return
     */
    public List<TariffVO> findTariffByContract(String defaultContractId, BizVO billVO) {
        Tariff tariff = new Tariff();
        tariff.setTenancyId(billVO.getTenancyId());
        tariff.setContractId(defaultContractId);
        tariff.setDelFlag(0);
        //tariff.setConfirmStatus(1);
        String sql = tariffRepo.getSelectByEntity(tariff, false);
        sql = StrUtil.join(sql, " and CHARGING_ID in (", billVO.getItemIds(),")");
        //sql = StrUtil.join(sql, " and EXISTS (SELECT 1 FROM C_TARIFF_RULE WHERE id=FILTER_ID)");
        return tariffVORepo.list(sql, TariffVO.class);
    }

    /**
     * 合同号 + 费目编号 = 查找所有费率条件
     * @param defaultContractId
     * @param billVO
     * @return
     */
    public List<TariffRitem> findRitemByTariff(String defaultContractId, BizVO billVO) {
        String sql = StringUtils.join("select a.* from C_TARIFF_RITEM a, C_TARIFF b ",
                " where a.RULE_ID = b.ID and b.CONTRACT_ID=? and b.CHARGING_ID in(",
                billVO.getItemIds(),") and b.DEL_FLAG=0");
        if (!StrUtil.isEmpty(billVO.getTenancyId())){
            sql = StrUtil.join(sql, " and b.TENANCY_ID='", billVO.getTenancyId(),"'");
        }
        return ritemRepo.list(sql, TariffRitem.class, defaultContractId);
    }



    public List<ItemProperties> findPropertiesByItems(BizVO billVO) {
        ItemProperties properties = new ItemProperties();
        properties.setTenancyId(billVO.getTenancyId());
        String sql = itemPropertiesRepo.getSelectByEntity(properties, false);
        sql = StrUtil.join(sql, " and ITEM_ID in (", billVO.getItemIds(),")");
        return itemPropertiesRepo.list(sql, ItemProperties.class);
    }


    public List<TariffMultistep> findStepByTariff(String defaultContractId, BizVO billVO) {
        String sql = StringUtils.join("select a.* from C_TARIFF_MULTISTEP a, C_TARIFF b ",
                " where a.TARIFF_ID = b.ID and b.CONTRACT_ID=? and b.CHARGING_ID in(", billVO.getItemIds(),
                ") and b.DEL_FLAG=0");
        if (!StrUtil.isEmpty(billVO.getTenancyId())){
            sql = StrUtil.join(sql, " and b.TENANCY_ID='", billVO.getTenancyId(),"'");
        }
        return stepORepo.list(sql, TariffMultistep.class, defaultContractId);
    }

    public List<TariffMultistep> findCustomStepByTariff(String defaultContractId, BizVO billVO) {
        String sql = StringUtils.join("select * from C_TARIFF_MULTISTEP ",
                " where TARIFF_ID in (select ID from C_TARIFF " ,
                " where TENANCY_ID=? and CONTRACT_ID=? and CHARGING_ID in (", billVO.getItemIds(),
                ") and CUSTOMERS like '%", billVO.getShipperCode() , "%' and RATE_TYPE=1) and TENANCY_ID=?");

        return stepORepo.list(sql, TariffMultistep.class, billVO.getTenancyId(), defaultContractId,
                billVO.getTenancyId());
    }

    public ContractVO findContractId(String defaultContractId, BizVO bizVO) {
        //String sql = "select id from C_Contract where id in ('"+defaultContractId+"') and TENANCY_ID=?";
        Contract contract = new Contract();
        contract.setTenancyId(bizVO.getTenancyId());
        String sql = contractRepo.getSelectByEntity(contract, true);
        sql = StrUtil.join(sql, " and id in (", defaultContractId, ")");
        sql = StrUtil.join(sql, " and EFFECTIVE_DATE<=", TimeUtil.toOrclDate(bizVO.getBizTime()));
        sql = StrUtil.join(sql, " and EXPIRED_DATE>=", TimeUtil.toOrclDate(bizVO.getBizTime()));
        return contractVORepo.get(sql, ContractVO.class);
    }
}
