package com.ag.rate.tariff.dao;

import com.ag.base.IBaseRepository;
import com.ag.entity.fee.*;
import com.ag.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * Created by Administrator on 2017/4/11 0011.
 */
@Repository
public class TariffDao {

    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<BaseCategory> cateRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<BaseChargingItem> itemRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<Tariff> tariffRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<TariffRitem> ritemRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<TariffMultistep> stepRepo;
    @Resource(name="baseRepositoryImpl")
    private IBaseRepository<ContractItem> contItemRepo;


    public String findAll(BaseCategory cate) {
        return cateRepo.findByEqSql(cate, "TENANCY_ID");
    }

    public int save(BaseCategory cate) {
        return cateRepo.save(cate);
    }

    public int update(BaseCategory cate, String[] requestParam) {
        return cateRepo.update(cate, requestParam, "ID");
    }

    public int delete(BaseCategory cate) {
        return cateRepo.delete(cate, "ID");
    }

    public int isUseItemByCate(String id) {
        String sql = "select count(0) from C_BASE_CHARGING_ITEM where CATEGORY_ID=?";
        return cateRepo.count(sql, id);
    }

    public String findAll(BaseChargingItem item) {
        String sql = itemRepo.findByEqSql(item, "TENANCY_ID");
        /*sql = StrUtil.join(sql, " and EXISTS (select * from C_CATEGORY_CONTACT where TENANCY_ID='",item.getTenancyId(),
                "' and CHARGING_ID=a.id");*/
        if (!sql.toLowerCase().contains(" where ")){
            sql = StrUtil.join(sql, " where 1=1 ");
        }
        if (!StrUtil.isEmpty(item.getCategoryId())){
            sql = StrUtil.join(sql, " and CATEGORY_ID='", item.getCategoryId(),"'");
        }
        //sql = StrUtil.join(sql, ")");
        return sql;
    }

    public List<BaseChargingItem> findBaseItemLst(BaseChargingItem item){
        return itemRepo.findByEntity(item, BaseChargingItem.class);
    }

    public int save(BaseChargingItem item) {
        return itemRepo.save(item);
    }

    public int update(BaseChargingItem item, String[] requestParam) {
        return itemRepo.update(item, requestParam,"ID");
    }

    public int delete(BaseChargingItem item) {
        return itemRepo.delete(item, "ID");
    }




    public String findAll(TariffRitem ritem) {
        return ritemRepo.findByEqSql(ritem, "RULE_ID");
    }

    public int save(TariffRitem ritem) {
        return ritemRepo.save(ritem);
    }

    public int update(TariffRitem ritem) {
        return ritemRepo.update(ritem, "ID");
    }
    public int[] updateRitem(List<TariffRitem> ritems) {
        return ritemRepo.updateAll(ritems, "ID");
    }

    public int deleteItemByRuleId(String ruleId) {
        String sql = "delete from C_TARIFF_RITEM where RULE_ID=?";
        return ritemRepo.update(sql, ruleId);
    }

    public String findTariffByCategory(Tariff tariff) {
        //return tariffRepo.findByEqSql(tariff, Tariff.class, "TENANCY_ID","CHARGING_ID");
        String sql = StringUtils.join("SELECT a.ID, a.TENANCY_ID, a.FILTER_ID, b.DESCRIPTION FILTER_NAME, a.CHARGING_ID,",
                " a.RATE, a.DISCOUNT, a.CURRENCY, a.GL_CODE, a.RATE_TYPE, a.FLAT_RATE, a.MIN_AMOUNT, a.MAX_AMOUNT, a.START_FIELD, ",
                "a.END_FIELD, a.IS_VACATION, a.IS_NIGHTSHIFT, a.IS_WEEKDAY, a.IS_TIMES, a.TIME_UNIT, a.DESCRIPTION, a.NOTES, ",
                "a.CREATED_BY, a.CREATED_ON, a.UPDATED_BY, a.UPDATED_ON, a.CUSTOMERS, a.CUSTOMER_NAM, a.VOL_PRICE FROM C_TARIFF a, C_TARIFF_RULE b ",
                "where a.FILTER_ID = b.ID and a.TENANCY_ID='", tariff.getTenancyId(), "' and a.CHARGING_ID='",
                tariff.getChargingId(), "' and a.CONTRACT_ID='", tariff.getContractId(), "'");
        /*if(!StringUtils.isEmpty(tariff.getDescription())){
            String[] array = tariff.getDescription().split(",");
            for (String descript : array){
                sql = StringUtils.join(sql, " and a.DESCRIPTION like '%", StrUtil.trimString(descript), "%'");
            }
        }*/
        return sql;
    }

    public int save(Tariff tariff) {
        return tariffRepo.save(tariff);
    }

    public int update(Tariff tariff, String[] requestParam) {
        return tariffRepo.update(tariff, requestParam,"ID");
    }

    public int delete(Tariff tariff) {
        return tariffRepo.delete(tariff, "ID");
    }

    public List<Tariff> findByContract(Tariff tariff) {
        return tariffRepo.findByEntity(tariff, Tariff.class);
    }
    public List<Tariff> findByItemIdAndContractId(Tariff tariff) {
        return tariffRepo.findByEntity(tariff, Tariff.class);
    }

    public int[] saveTariff(List<Tariff> tariffLst) {
        return tariffRepo.saveAll(tariffLst);
    }

    public List<BaseCategory> findByInIds(String saveIds, BaseCategory cate) {
        String sql = findAll(cate);
        if (!sql.toLowerCase().contains(" where ")){
            sql = StrUtil.join(sql, " where 1=1 ");
        }
        sql = StrUtil.join(sql, " and id in('", saveIds ,"') and ENABLED=1 ");
        return cateRepo.list(sql, BaseCategory.class);
    }

    public List<BaseChargingItem> findByInIds(String saveIds, BaseChargingItem item) {
        String sql = findAll(item);
        if (!sql.toLowerCase().contains(" where ")){
            sql = StrUtil.join(sql, " where 1=1 ");
        }
        sql = StrUtil.join(sql, " and id in('", saveIds ,"') and LIFE_STATUS=1 ");
        return itemRepo.list(sql, BaseChargingItem.class);
    }

    public Integer findTariffNum(Tariff tariff) {
        String sql = "select count(0) from C_TARIFF where CHARGING_ID=? and CONTRACT_ID=? and TENANCY_ID=?";
        return tariffRepo.count(sql, tariff.getChargingId(), tariff.getContractId(), tariff.getTenancyId());
    }

    public String findMultistep(TariffMultistep step) {
        return stepRepo.getSelectByEntity(step, false);
    }

    public void saveStep(List<TariffMultistep> createLst) {
        stepRepo.saveAll(createLst);
    }

    public void updateStep(List<TariffMultistep> updateLst, String[] needCols) {
        stepRepo.updateAll(updateLst, needCols, "ID");
    }

    public void deleteStep(List<TariffMultistep> destroyLst) {
        stepRepo.deleteAll(destroyLst, "ID");
    }

    public Integer queryUsedTariff(String id, String tenancyId) {
        String sql = "select count(0) from C_TARIFF a, C_CONTRACT_ITEM b where a.FILTER_ID=? and a.TENANCY_ID=? and a.CHARGING_ID = b.ITEM_ID";
        return tariffRepo.count(sql, id, tenancyId);
    }


    public int updateCategory(ContractItem entity) {
        return contItemRepo.update(entity, "ITEM_ID");
    }

    public List<TariffRitem> findRitemsByContract(Tariff contract) {
        String tsql = "select id from C_TARIFF where CONTRACT_ID=?";
        String sql = "select * from C_TARIFF_RITEM where RULE_ID in("+tsql+")";
        return ritemRepo.list(sql, TariffRitem.class, contract.getContractId());
    }
    public void deleteRitemsByContract(String contractId) {
        String tsql = "select id from C_TARIFF where CONTRACT_ID=?";
        String sql = "update C_TARIFF_RITEM set DEL_FLAG = 1 where RULE_ID in("+tsql+")";
        ritemRepo.update(sql,contractId);
    }
    public void deleteRitemsByContractItem(ContractItem contractItem) {
        String tsql = "select id from C_TARIFF where CONTRACT_ID=? and CHARGING_ID = ?";
        String sql = "update C_TARIFF_RITEM set DEL_FLAG = 1 where RULE_ID in("+tsql+")";
        ritemRepo.update(sql,contractItem.getContractId(),contractItem.getItemId());
    }
    public List<TariffMultistep> findStepByContract(Tariff contract) {
        String tsql = "select id from C_TARIFF where CONTRACT_ID=?";
        String sql = "select * from C_TARIFF_MULTISTEP where TARIFF_ID in("+tsql+")";
        return stepRepo.list(sql, TariffMultistep.class, contract.getContractId());
    }
    public void deleteStepByContract(String contractId) {
        String tsql = "select id from C_TARIFF where CONTRACT_ID=?";
        String sql = "update C_TARIFF_MULTISTEP set DEL_FLAG = 1 where TARIFF_ID in("+tsql+")";
        stepRepo.update(sql, contractId);
    }
    public void deleteStepByContractItem(ContractItem contractItem) {
        String tsql = "select id from C_TARIFF where CONTRACT_ID=? and CHARGING_ID = ?";
        String sql = "update C_TARIFF_MULTISTEP set DEL_FLAG = 1 where TARIFF_ID in("+tsql+")";
        stepRepo.update(sql,contractItem.getContractId(),contractItem.getItemId());
    }

    public int[] saveRitem(List<TariffRitem> ritems) {
        return ritemRepo.saveAll(ritems);
    }

    public void deleteTariff(String contractId) {
        String sql = "update C_TARIFF set DEL_FLAG = 1 where CONTRACT_ID=?";
        tariffRepo.update(sql,contractId);
    }
    public void deleteTariffByContractItem( ContractItem contractItem) {
        String sql = "update C_TARIFF set DEL_FLAG = 1 where CONTRACT_ID=? and CHARGING_ID = ?";
        tariffRepo.update(sql,contractItem.getContractId(),contractItem.getItemId());
    }
}

