package com.ag.rate.tariff.dao;

import com.ag.base.IBaseRepository;
import com.ag.entity.fee.Contract;
import com.ag.entity.fee.ContractCategory;
import com.ag.entity.fee.ContractItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * Created by Administrator on 2017/6/3 0003.
 */
@Repository
public class ContractDao {

    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<Contract> contractRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ContractCategory> contCateRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ContractItem> contItemRepo;

    public String findAll(Contract contract) {
        return contractRepo.findByEqSql(contract, "TENANCY_ID");
    }

    public int save(Contract contract) {
        return contractRepo.save(contract);
    }

    public int update(Contract contract, String[] requestParam) {
        return contractRepo.update(contract, requestParam,"ID");
    }

    public int delete(Contract contract) {
        return contractRepo.delete(contract, "ID");
    }

    public String queryCateByContract(ContractCategory contractCategory) {
        return contCateRepo.findByEqSql(contractCategory, ContractCategory.class, "TENANCY_ID","CONTRACT_ID");
        /*String sql = StringUtils.join("select b.ID, b.CATEGORY_ID,",
                "a.DESCRIPTION, a.IS_ALL, a.EFFECTIVED, a.ENABLED, a.BIZ_SCOPE, a.NOTES, b.CREATEUSER, b.CREATETIME",
                " from C_BASE_CATEGORY a,C_CONTRACT_CATEGORY b where a.ID = b.CATEGORY_ID and a.TENANCY_ID='",
                contractCategory.getTenancyId(), "' and b.CONTRACT_ID='", contractCategory.getContractId(), "'");
        return sql;*/
    }

    public int save(ContractCategory contractCategory) {
        return contCateRepo.save(contractCategory);
    }

    public int update(ContractCategory contractCategory, String[] requestParam) {
        return contCateRepo.update(contractCategory, requestParam,"ID");
    }

    public int delete(ContractCategory contractCategory) {
        return contCateRepo.delete(contractCategory, "CONTRACT_ID");
    }

    public String queryItemByCate(ContractItem item) {
        return contItemRepo.findByEqSql(item, ContractItem.class, "TENANCY_ID","CONTRACT_ID","CATEGORY_ID");
        /*String sql = StringUtils.join("select b.ID, b.ITEM_ID, b.CREATEUSER, b.CREATETIME, ",
                "a.DESCRIPTION, a.INVOICE_TYPE, a.RATE_TYPE, a.LIFE_STATUS, a.MIN_AMOUNT, a.MAX_AMOUNT, a.UNIT, ",
                "a.IS_AUTOCALC, a.IS_FORMAT, a.FORMAT_UNIT, a.CURRENCY, a.GL_CODE, a.FLAT_RATE, a.IS_ALL, " ,
                        "a.ACCOUNT_TYPE, a.IS_AVERAGE, a.NOTES ",
                " from C_BASE_CHARGING_ITEM a,C_CONTRACT_ITEM b where a.ID = b.ITEM_ID and a.TENANCY_ID='",
                item.getTenancyId(), "' and b.CONTRACT_ID='", item.getContractId(), "' and b.CATEGORY_ID='",
                item.getCategoryId(), "'");
        return sql;*/
    }

    public void deleteSql(ContractItem delCI) {
        String sql = "delete from C_CONTRACT_ITEM where CONTRACT_ID=? and CATEGORY_ID=?";
        contItemRepo.update(sql, delCI.getContractId(), delCI.getCategoryId());
    }
    public void deleteContractItem(String contractId) {
        String sql = "update C_CONTRACT_ITEM set DEL_FLAG = 1 where CONTRACT_ID=?";
        contItemRepo.update(sql,contractId);
    }
    public void save(ContractItem contractItem) {
        contItemRepo.save(contractItem);
    }

    public List<ContractCategory> findByContract(ContractCategory contractCategory) {
        return contCateRepo.findByEntity(contractCategory, ContractCategory.class);
    }

    public List<ContractItem> findByContract(ContractItem contractItem) {
        return contItemRepo.findByEntity(contractItem, ContractItem.class);
    }



    public String queryContractByCategory(ContractCategory contractCategory) {
        return StringUtils.join("select a.ID,a.DESCRIPTION from C_CONTRACT a, C_CONTRACT_CATEGORY b",
                " where a.ID=b.CONTRACT_ID and CATEGORY_ID='", contractCategory.getCategoryId(),
                "' and a.TENANCY_ID='", contractCategory.getTenancyId(), "'");
    }

    public int[] saveContCate(List<ContractCategory> ccLst) {
        return contCateRepo.saveAll(ccLst);
    }

    public int[] delContCate(List<ContractCategory> ccLst, Object... colNames) {
        return contCateRepo.deleteAll(ccLst, colNames);
    }


    public int[] saveContItem(List<ContractItem> ciLst) {
        return contItemRepo.saveAll(ciLst);
    }
    public int[] delContItem(List<ContractItem> ciLst, Object... colNames) {
        return contItemRepo.deleteAll(ciLst, colNames);
    }

    public int update(ContractItem entity, String[] requestParam) {
        return contItemRepo.update(entity, requestParam,"ID");
    }
}
