package com.sifang.modules.sunshine.SystemSetting.service;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.platform.entity.CrmSiteReceivingInfo;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmSiteCategoryDao;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmSiteOrderDefaultAddressDao;
import com.sifang.modules.sunshine.SystemSetting.dao.SiteInformationDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSite;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSiteCategory;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmSiteOrderDefaultAddress;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.utils.RandomUtil;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.platform.dao.RecevierAddressDao;
import com.sifang.modules.order.utils.Apiutils;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.util.http.EzTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SiteInformationService extends BaseService {
    @Autowired
    private SiteInformationDao siteInformationDao;
    @Autowired
    private CrmSiteOrderDefaultAddressDao crmSiteOrderDefaultAddressDao;
    @Autowired
    private CrmSiteCategoryDao crmSiteCategoryDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RecevierAddressDao recevierAddressDao;

    @Autowired
    private EzTemplate template;

    public CrmSite getById(String id){
        return siteInformationDao.get(id);
    }


    //获取充值记录数据
    public void getSite(LayuiPage page,Map<String, String> map){
        List<Record> list = siteInformationDao.getSite(page,map);
        Long count = siteInformationDao.getCount(map);
        page.setCount(count);
        page.setData(list);
    }

    /**
     * 获取CRM客户信息
    */
    public List<Map<String,Object>> getCustomerMap(){
        try {
            List<String> cuss = Db.query("SELECT customer_id FROM crm_site WHERE is_crm_customer='2' AND STATUS !='1' ");
            String url = "/welcome/getAllCustomer";
            Map<String,Object> map = template.postForm(url, new HashMap<String, Object>(), new ParameterizedTypeReference<Map<String, Object>>() {
            });
            logger.info(" 获取CRM客户信息 map : {}",map);
            List<Map<String,Object>> mapss = Lists.newArrayList();
            if(map != null){
                List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("data");
                if(list != null){
                    for(Map<String,Object> rd:list){
                        String cusId = ((Map<String,Object>)rd.get("columns")).get("id").toString();
                        for(String ids:cuss){
                            if(ids.equals(cusId)){
                                mapss.add(rd);
                            }
                        }

                    }
                }
                if(mapss.size()==0){
                    return list;
                }
                return listrem(list,mapss);
            }
        }catch (Exception e){
            logger.error("获取CRM客户信息  Exception: {}",e);
           e.printStackTrace();
           System.out.println(e);
        }
    return null;
    }

    /**
     * 获取所有的CRM客户信息
     */
    public List<Map<String,Object>> getAllCustomerMap(){
        try {
            String url = "/welcome/getAllCustomer";
            Map<String,Object> map = template.postForm(url, null, new ParameterizedTypeReference<Map<String, Object>>() {
            });
            if(map != null){
                List<Map<String,Object>> list = (List<Map<String,Object>>) map.get("data");

                return list;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e);
        }
        return null;
    }

    /***　　这是目前我了解到速度最快的一种*/@SuppressWarnings("unchecked")
    public static List<Map<String,Object>> listrem(List<Map<String,Object>> listA,List<Map<String,Object>> listB){
        HashSet hs1 = new HashSet(listA);
        HashSet hs2 = new HashSet(listB);
        hs1.removeAll(hs2);
        List<Map<String,Object>> listC = new ArrayList<Map<String,Object>>();
        listC.addAll(hs1);
        return listC;
    }

    public Result siteBinDingCustomer(String siteId,String customerId,String customerName){

        CrmSite site = siteInformationDao.get(siteId);
        User user =UserUtils.getUser();
        logger.info("账号为：{} , userId :{}  ,给服务商： {},siteId :{}， 绑定CRM客户信息 {} ,customerId :{}", user.getLoginName(),user.getId(),site.getName() ,site,customerName,customerId);
        site.setCustomerId(customerId);
        site.setCustomerName(customerName);
        site.setIsCrmCustomer(SourceConstants.TWO);
        siteInformationDao.save(site);

        String url ="/welcome/findCustomerMoney";
        Map<String,Object> map = new HashMap<>();
        map.put("customerId",customerId);
        map.put("siteId",siteId);
        Map<String,Object> jieMap=	template.postForm(url, map, new ParameterizedTypeReference<Map<String,Object>>() {
        });

        return Result.ok();
    }

    public void Unbundling(String siteId){
        CrmSite site = siteInformationDao.get(siteId);
        String customerId = site.getCustomerId();
        if(StringUtil.isNotBlank(site.getId())){
            site.setIsCrmCustomer(SourceConstants.ONE);
            site.setCustomerName("");
            site.setCustomerId("");
            siteInformationDao.save(site);

            String url ="/welcome/resetCustomerMoney";
            Map<String,Object> map = new HashMap<>();
            map.put("customerId",customerId);
            Map<String,Object> jieMap=	template.postForm(url, map, new ParameterizedTypeReference<Map<String,Object>>() {
            });
        }
        User user =UserUtils.getUser();
        logger.info("账号为：{} , userId :{}  ,给服务商： {},siteId :{}， 解除绑定CRM客户信息 ", user.getLoginName(),user.getId(),site.getName() ,site);
    }



    /**
     * 获取默认的服务商地址
     * @param id
     * @return
     */
    public Record getCurrentSiteAddress(String id){
        return siteInformationDao.getCurrentSiteAddress(id);
    }

    /**
     * 添加地址保存
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object addRessSave(Map<String,String> map){
        Result result = new Result();
        try{
            String siteId = map.get("id") !=null ? map.get("id").toString() : "";
            String province = map.get("province") !=null ? map.get("province").toString() : "";
            String city = map.get("city") !=null ? map.get("city").toString() : "";
            String area = map.get("area") !=null ? map.get("area").toString() : "";
            String address = map.get("address") !=null ? map.get("address").toString() : "";
            Record rd = Db.findFirst("select * from crm_site_order_default_address d where d.site_id=? ",siteId);
            if(rd !=null){
                String id = rd.getStr("id");
                CrmSiteOrderDefaultAddress siteOrderDefaultAddress =  crmSiteOrderDefaultAddressDao.get(id);
                if(siteOrderDefaultAddress != null){
                    siteOrderDefaultAddress.setProvince(province);
                    siteOrderDefaultAddress.setCity(city);
                    siteOrderDefaultAddress.setArea(area);
                    siteOrderDefaultAddress.setAddress(address);
                    String datailsAddress = siteOrderDefaultAddress.getProvince()+siteOrderDefaultAddress.getCity()+siteOrderDefaultAddress.getArea()+siteOrderDefaultAddress.getAddress();
                    double[] dals = Apiutils.addressToGPS(datailsAddress);
                    if(dals != null){
                        siteOrderDefaultAddress.setLnglat(Double.toString(dals[0])+","+Double.toString(dals[1]));
                    }
                    crmSiteOrderDefaultAddressDao.save(siteOrderDefaultAddress);
                    logger.info("保存CrmSiteOrderDefaultAddress表数据成功："+siteOrderDefaultAddress+",saveTime:"+ DateUtils.getDateTime());
                    result.setCode("200");
                }
            }else{
                CrmSiteOrderDefaultAddress siteAddress = new CrmSiteOrderDefaultAddress();
                siteAddress.setSiteId(siteId);
                siteAddress.setProvince(province);
                siteAddress.setCity(city);
                siteAddress.setArea(area);
                siteAddress.setAddress(address);
                String datailsAddress = siteAddress.getProvince()+siteAddress.getCity()+siteAddress.getArea()+siteAddress.getAddress();
                double[] dals = Apiutils.addressToGPS(datailsAddress);
                if(dals != null){
                    siteAddress.setLnglat(Double.toString(dals[0])+","+Double.toString(dals[1]));
                }
                crmSiteOrderDefaultAddressDao.save(siteAddress);
                logger.info("保存CrmSiteOrderDefaultAddress表数据成功："+siteAddress+",saveTime:"+ DateUtils.getDateTime());
                result.setCode("200");
            }
        }catch (Exception e){
            result.setCode("205");
            logger.info("返回异常信息:"+e+",saveTime:"+ DateUtils.getDateTime());
        }
        return result;
    }

    /**
     * 保存服务商
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object saveSite(Map<String,String> map){
        Result result = new Result();
        try{
            String id = map.get("id") !=null ? map.get("id").toString() : "";
            String name = map.get("siteName") !=null ? map.get("siteName").toString() : "";
            String loginName = map.get("loginName") !=null ? map.get("loginName").toString() : "";
            String password = map.get("password") !=null ? map.get("password").toString() : "";
            String contacts = map.get("userName") !=null ? map.get("userName").toString() : "";
            String telephone = map.get("telephone") !=null ? map.get("telephone").toString() : "";
            String province = map.get("province") !=null ? map.get("province").toString() : "";
            String city = map.get("city") !=null ? map.get("city").toString() : "";
            String area = map.get("area") !=null ? map.get("area").toString() : "";
            String address = map.get("address") !=null ? map.get("address").toString() : "";
            String imgs = map.get("imgs") !=null ? map.get("imgs").toString() : "";
            CrmSite site = getById(id);
                if(site != null){
                    User user = userDao.get(site.getUserId());
                    if (user==null) {
                        user = new User();
                        if(siteInformationDao.getExistsLoginName(loginName)){
                            result.setCode("201");
                            return result;
                        }
                        user.setMobile(telephone);
                        user.setCreateBy(UserUtils.getUser().getId());
                        user.setStatus(UserConstants.USER_STATUS_NORMAL);
                        user.setUserType(UserConstants.TYPE_SIT);
                        user.setCreateDate(new Date());
                    }
                    if (siteInformationDao.getExistsLoginNameNew(user.getId(),loginName)) {
                        result.setCode("201");
                        return result;
                    }
                    user.setLoginName(loginName);
                    if (!password.equals("default")) {
                        if (!user.getPassword().equals(password)) {
                            user.setPassword(RandomUtil.entryptPassword(password));
                        }
                    }

                    userDao.save(user);
                    site.setName(name);
                    site.setContacts(contacts);
                    site.setTelephone(telephone);
                    site.setProvince(province);
                    site.setCity(city);
                    site.setArea(area);
                    site.setAddress(address);
                    site.setUpdateTime(new Date());
                    site.setLicenseImg(imgs);
                    String datailsAddress = site.getProvince()+site.getCity()+site.getArea()+site.getAddress();
                    double[] dals = Apiutils.addressToGPS(datailsAddress);
                    if(dals != null){
                        site.setLnglat(Double.toString(dals[0])+","+Double.toString(dals[1]));
                    }
                    siteInformationDao.save(site);
                    logger.info("保存Crmsite表数据成功："+site+",saveTime:"+ DateUtils.getDateTime());
                    result.setCode("200");
                } else{
                    if(siteInformationDao.getExistsLoginName(loginName)){
                        result.setCode("201");
                        return result;
                    }
                    if(siteInformationDao.getExistsSiteName(name)){
                        result.setCode("202");
                        return result;
                    }
                    User user = new User();
                    user.setCreateBy(UserUtils.getUser().getId());
                    user.setStatus(UserConstants.USER_STATUS_NORMAL);
                    user.setUserType(UserConstants.TYPE_SIT);
                    user.setCreateType(1);
                    user.setCreateDate(new Date());
                    user.setLoginName(loginName);
                    user.setPassword(RandomUtil.entryptPassword(password));
                    userDao.save(user);
                    site = new CrmSite();
                    site.setUserId(user.getId());
                    site.setStatus(UserConstants.USER_STATUS_NORMAL);
                    site.setCreateBy(UserUtils.getUser().getId());
                    site.setCreateTime(new Date());
                    site.setCheckFlag(SourceConstants.ONE);
                    site.setSiteType(SourceConstants.TWO);
                    site.setName(name);
                    site.setContacts(contacts);
                    site.setTelephone(telephone);
                    site.setMobile(telephone);
                    site.setProvince(province);
                    site.setCity(city);
                    site.setArea(area);
                    site.setAddress(address);
                    site.setLicenseImg(imgs);
                    site.setIsCrmCustomer(SourceConstants.ONE);
                    String datailsAddress = site.getProvince()+site.getCity()+site.getArea()+site.getAddress();
                double[] dals = Apiutils.addressToGPS(datailsAddress);
                if(dals != null){
                    site.setLnglat(Double.toString(dals[0])+","+Double.toString(dals[1]));
                }
                siteInformationDao.save(site);
                logger.info("保存CrmSiteOrderDefaultAddress表数据成功："+site+",saveTime:"+ DateUtils.getDateTime());
                // 默认添加服务商收货地址
                    CrmSiteReceivingInfo info = new CrmSiteReceivingInfo();
                    info.setProvince(site.getProvince());
                    info.setCity(site.getCity());
                    info.setArea(site.getArea());
                    info.setAddress(site.getAddress());
                    info.setReceiver(site.getContacts());
                    info.setSiteId(site.getId());
                    info.setMobile(telephone);
                    info.setDafaultAddress(SourceConstants.ZERO);
                    info.setReleType(SourceConstants.ONE);
                    info.setCreateTime(new Date());
                    recevierAddressDao.save(info);
                result.setCode("200");
            }
        }catch (Exception e){
            result.setCode("205");
            logger.info("返回异常信息:"+e+",saveTime:"+ DateUtils.getDateTime());
        }
        return result;
    }

    /**
     * 同步代理商信息到服务商
    */
    public void tongbuSite(CrmTrader trader){
       CrmSite site = new CrmSite();
        site.setUserId(trader.getUserId());
        site.setStatus(UserConstants.USER_STATUS_NORMAL);
        site.setCreateBy(UserUtils.getUser().getId());
        site.setCreateTime(new Date());
        site.setCheckFlag(SourceConstants.ONE);
        site.setSiteType(SourceConstants.TWO);
        site.setName(trader.getName());
        site.setContacts(trader.getContacts());
        site.setTelephone(trader.getTelephone());
        site.setMobile(trader.getTelephone());
        site.setProvince(trader.getProvince());
        site.setCity(trader.getCity());
        site.setArea(trader.getArea());
        site.setAddress(trader.getAddress());
        site.setLicenseImg(trader.getLicenseImg());
        site.setIsCrmCustomer(SourceConstants.ONE);
        String datailsAddress = site.getProvince()+site.getCity()+site.getArea()+site.getAddress();
        double[] dals = Apiutils.addressToGPS(datailsAddress);
        if(dals != null){
            site.setLnglat(dals[0]+","+dals[1]);
        }
        siteInformationDao.save(site);
        logger.info("保存CrmSiteOrderDefaultAddress表数据成功："+site+",saveTime:"+ DateUtils.getDateTime());
        // 默认添加服务商收货地址
        CrmSiteReceivingInfo info = new CrmSiteReceivingInfo();
        info.setProvince(site.getProvince());
        info.setCity(site.getCity());
        info.setArea(site.getArea());
        info.setAddress(site.getAddress());
        info.setReceiver(site.getContacts());
        info.setSiteId(site.getId());
        info.setMobile(trader.getMobile());
        info.setDafaultAddress(SourceConstants.ZERO);
        info.setReleType(SourceConstants.ONE);
        info.setCreateTime(new Date());
        recevierAddressDao.save(info);
    }


    /**
     * 添加品类保存
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object addCategorySave(String jsonData,String serviceType,String siteId){
        Result result = new Result();
        List<CrmSiteCategory> list = Lists.newArrayList();
        try{
            Record rdNew = Db.findFirst("SELECT * FROM crm_site_category p WHERE p.del_flag = '0' and p.site_id =? and p.service_type=?",siteId,serviceType);
            if(rdNew !=null){
                int id = rdNew.getInt("id");
                CrmSiteCategory crmSiteCategory = crmSiteCategoryDao.get(id);
                crmSiteCategory.setName(jsonData);
               crmSiteCategoryDao.save(crmSiteCategory);
                logger.info("保存CrmSiteCategory表数据成功："+list+",saveTime:"+ DateUtils.getDateTime());
            }else{
                CrmSiteCategory siteCategory = new CrmSiteCategory();
                User user = UserUtils.getUser();
                String userId = user.getId();
                Record rd = Db.findFirst("SELECT MAX(sort) AS sort FROM crm_site_category p WHERE p.del_flag = '0' ");
                int sort =  rd.getInt("sort") !=null ? rd.getInt("sort") : 0;
                siteCategory.setName(jsonData);
                siteCategory.setServiceType(serviceType);
                siteCategory.setImg("");
                siteCategory.setSort(sort+1);
                siteCategory.setDelFlag("0");//删除标示,0 正常， 1 已删除
                siteCategory.setCreateTime(new Date());
                siteCategory.setSiteId(siteId);
                siteCategory.setCreateBy(userId);
                siteCategory.setCategoryName("");
                list.add(siteCategory);
           crmSiteCategoryDao.save(list);
                logger.info("保存CrmSiteCategory表数据成功："+list+",saveTime:"+ DateUtils.getDateTime());
            }
            result.setCode("200");
        }catch (Exception e){
            result.setCode("205");
            logger.info("返回异常信息:"+e+",saveTime:"+ DateUtils.getDateTime());
            }
        return result;
    }


    /**
     * 获取默认的里程地址地址
     * @param id
     * @return
     */
    public Record getCurrentSiteEditorAddress(String id){
        return siteInformationDao.getCurrentSiteEditorAddress(id);
    }


    /**
     * 修改地址后保存
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object editorAddressSave(Map<String,String> map){
        Result result = new Result();
        try{
            String siteId = map.get("id") !=null ? map.get("id").toString() : "";
            String province = map.get("province") !=null ? map.get("province").toString() : "";
            String city = map.get("city") !=null ? map.get("city").toString() : "";
            String area = map.get("area") !=null ? map.get("area").toString() : "";
            String address = map.get("address") !=null ? map.get("address").toString() : "";
            Record rd = Db.findFirst("select d.id from crm_site_order_default_address d where d.site_id =? ",siteId);
            String id = rd.getStr("id");
            CrmSiteOrderDefaultAddress siteAddress = crmSiteOrderDefaultAddressDao.get(id);
            if(siteAddress != null){
                siteAddress.setProvince(province);
                siteAddress.setCity(city);
                siteAddress.setArea(area);
                siteAddress.setAddress(address);
                String datailsAddress = siteAddress.getProvince()+siteAddress.getCity()+siteAddress.getArea()+siteAddress.getAddress();
                double[] dals = Apiutils.addressToGPS(datailsAddress);
                if(dals != null){
                    siteAddress.setLnglat(Double.toString(dals[0])+","+Double.toString(dals[1]));
                }
                crmSiteOrderDefaultAddressDao.save(siteAddress);
                logger.info("保存CrmSiteOrderDefaultAddress表数据成功："+siteAddress+",saveTime:"+ DateUtils.getDateTime());
                result.setCode("200");
            }
        }catch (Exception e){
            result.setCode("205");
            logger.info("返回异常信息:"+e+",saveTime:"+ DateUtils.getDateTime());
        }
        return result;
    }


    /**
     *修改品类后品类保存
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object editorCategorySave(String jsonData,String siteId,String serviceType){
        Result result = new Result();
       /* List<CrmSiteCategory> list = Lists.newArrayList();*/
        try{
            Record rd = Db.findFirst("select p.id from crm_site_category p where p.site_id =? and p.service_type=?",siteId,serviceType);
            int id = rd.getInt("id");
            CrmSiteCategory crmSiteCategory =  crmSiteCategoryDao.get(id);
                crmSiteCategory.setName(jsonData);
               /* list.add(crmSiteCategory);*/
            crmSiteCategoryDao.save(crmSiteCategory);
            logger.info("保存CrmSiteCategory表数据成功："+crmSiteCategory+",saveTime:"+ DateUtils.getDateTime());
            result.setCode("200");
        }catch (Exception e){
            result.setCode("205");
            logger.info("返回异常信息:"+e+",saveTime:"+ DateUtils.getDateTime());
        }
        return result;
    }

    /**
     * 获取市
     */
    public List<Record> getCitysByProId(){
        String sql = "SELECT c.CityID,c.CityName,c.ProvinceId,c.AreaCode FROM s_city c ";
        return Db.find(sql);
    }

    /**
     * 获取区县
     */
    public static List<Record> getAreaByCityId(){
        String sql = "SELECT d.DistrictID,d.DistrictName FROM s_district d ";
        return Db.find(sql);
    }

    public Result bindingSite(Map<String,String> map){
        Result result = new Result();
        try {
            String siteId = map.get("siteId");
            CrmSite si = siteInformationDao.get(siteId);
            if(si!= null){
                si.setStatus("0");
                siteInformationDao.save(si);
                return Result.fail("300","已存在信息");
            }
            if(StringUtil.isBlank(siteId)){
                return Result.fail("505","siteId信息为空");
            }
            CrmSite site = new CrmSite();
            site.setId(siteId);
            site.setUserId(map.get("user_id"));
            site.setNumber(map.get("number"));
            site.setName(map.get("name"));
            site.setContacts(map.get("contacts"));
            site.setCorporator(map.get("corporator"));
            site.setEmail(map.get("email"));
            site.setProvince(map.get("province"));
            site.setCity(map.get("city"));
            site.setArea(map.get("area"));
            site.setAddress(map.get("address"));
            site.setLnglat(map.get("lnglat"));
            site.setMobile(map.get("mobile"));
            site.setTelephone(map.get("telephone"));
            site.setLicenseNumber(map.get("license_number"));
            site.setLicenseImg(map.get("license_img"));
            site.setStatus("0");
            site.setCheckFlag("1");
            site.setRemarks(map.get("remarks"));
            site.setCreateBy(map.get("create_by"));
            site.setCreateTime(new Date());
            site.setUpdateTime(new Date());
            siteInformationDao.save(site);
            result.setCode("200");
        }catch (Exception e){
    e.printStackTrace();
            return Result.fail("505","报错");
        }

        return result;
    }

    /**
     * 解除绑定
    */
    public Result RelieveBindingSite(String siteId){
        try {
            CrmSite site = siteInformationDao.get(siteId);
            site.setStatus("3");//停用
            site.setUpdateTime(new Date());
            siteInformationDao.save(site);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("505","保存失败");
        }

        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    public Result deleteSite(String ids){
        String[] arr=ids.split(",");
        try{
            for(int i=0;i<arr.length;i++){
                String id=arr[i];
                if(StringUtil.isNotBlank(id)){
                    List<Record> list1 = siteInformationDao.getSiteOrder(id);
                    if (list1.size()>0) {
                        return Result.fail("501" ,"该服务商有未完成工单，无法删除。");
                    }
                    Record record = siteInformationDao.getSiteFitting(id);
                    if (record.getBigDecimal("warning")!=null&&record.getBigDecimal("warning").intValue()>0) {
                        return Result.fail("502" ,"该服务商有库存备件，无法删除。");
                    }
                    List<Record> list2 = siteInformationDao.getSettlement(id);
                    if (list2.size()>0) {
                        return Result.fail("503" ,"该服务商有未结算工单，无法删除。");
                    }
                    CrmSite site = getById(id);
                    site.setStatus("1");
                    site.setUpdateTime(new Date());
                    if (site.getUserId()!=null) {
                        siteInformationDao.save(site);
                        User user = userDao.getById(site.getUserId());
                        user.setStatus("1");
                        user.setUpdateTime(new Date());
                        userDao.save(user);
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            return Result.fail("504","删除失败");
        }
        return Result.ok();
    }


    public List<Record> getSiteRecevierAddress(String siteId){
        List<Record> list =recevierAddressDao.getSiteRecevierAddress(siteId);
        return list;
    }

}
