package com.sifang.modules.platform.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.entity.CrmSiteReceivingInfo;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.platform.entity.CrmSiteReceivingInfo;
import com.sifang.modules.sys.common.persistence.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 收货地址--维护 Dao
 */
@Repository
public class RecevierAddressDao extends BaseDao<CrmSiteReceivingInfo> {

    protected Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 获取收货地址列表数据
     * @param id
     * @return
     */
    public List<Record> getRecevierAddress(String id){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT s.* FROM crm_site_receiving_info s WHERE s.site_id = ? and rele_type ='0' ");
        sf.append(" ORDER BY s.dafault_address ASC  ");
        return Db.find(sf.toString(),id);
    }

    /**
     * 获取服务商收货地址列表数据
     * @param
     * @return
     */
    public List<Record> getSiteRecevierAddress(String siteId){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT s.id,s.receiver,s.mobile,s.province,s.city,s.area ,s.address,s.postal_code,s.dafault_address,s.site_id, CONCAT(province,city,AREA,address) AS siteAddress FROM crm_site_receiving_info s WHERE s.site_id = ? and rele_type ='1' ");
        sf.append(" ORDER BY s.dafault_address ASC ");
        return Db.find(sf.toString(),siteId);
    }

    /**
     * 获取服务商收货地址
     * @param
     * @return
     */
    public Record getAddress(String siteId){
        StringBuffer sf = new StringBuffer();
        sf.append("SELECT * FROM crm_site_receiving_info s WHERE s.site_id = ? and rele_type ='1' ");
        sf.append(" ORDER BY s.dafault_address ASC ");
        return Db.findFirst(sf.toString(),siteId);
    }

    /**
     * 新增后的删除操作
     * @param id
     */
    public void deleteBtn(String id){
        StringBuffer sf = new StringBuffer();
        sf.append("delete from crm_site_receiving_info where id=? ");
        Db.update(sf.toString(),id);
    }

    /**
     * 根据id获取当前数据
     * @param id
     * @return
     */
    public Record getDataList(String id){
        StringBuffer sf = new StringBuffer();
        sf.append("select f.* from crm_site_receiving_info f where f.id=? ");
        return Db.findFirst(sf.toString(),id);
    }

    /**
     * 设置默认地址为状态0
     * @param id
     * @return
     */
   public void setDafaultAddress(String id,String siteId){
        StringBuffer sf = new StringBuffer();
        sf.append("UPDATE crm_site_receiving_info SET dafault_address ='0' WHERE id= ? ");
      if(StringUtil.isNotBlank(siteId)){
            sf.append("  and rele_type ='1' and site_id = '"+siteId+"' ");
        }
       logger.info(sf.toString());
       Db.update(sf.toString(),id);
   }

    /**
     * 设置非默认地址状态为1
     * @param id
     * @return
     */
    public void setOtherAddress(String id,String siteId){
        StringBuffer sf = new StringBuffer();
        sf.append("UPDATE crm_site_receiving_info SET dafault_address ='1' WHERE  ");
        if(StringUtil.isNotBlank(siteId)){
            sf.append("   rele_type ='1' and site_id = '"+siteId+"' ");
        }
        logger.info("设置非默认地址状态为1   "+sf.toString());


        Db.update(sf.toString());

    }

}
