package com.sifang.modules.platform.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.platform.entity.CrmSiteReceivingInfo;
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.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.util.CrmUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收货地址--维护 service
 */
@Service
public class RecevierAddressService extends BaseService {
@Autowired
private RecevierAddressDao recevierAddressDao;

    /**
     * 获取收货地址列表数据
     * @param id
     * @return
     */
    public List<Record> getRecevierAddress(String id){
        return recevierAddressDao.getRecevierAddress(id);
    }
    /**
     * 获取服务商收货地址列表数据
     * @param id
     * @return
     */
    public List<Record> getSiteRecevierAddress(String id){
        return recevierAddressDao.getSiteRecevierAddress(id);
    }

    public Record getAddress(String id){
        return recevierAddressDao.getAddress(id);
    }

    /**
     * 新增保存
     * @param map
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object addSave(Map<String,String> map){
        Map<String,Object> mapRet = new HashMap<>();
        try{
            String receiver = map.get("receiver") !=null ? map.get("receiver").toString() : "";
            String mobile = map.get("mobile") !=null ? map.get("mobile").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 detailAddress = map.get("detailAddress") !=null ? map.get("detailAddress").toString() : "";
            String postCode = map.get("postCode") !=null ? map.get("postCode").toString() : "";
            String siteId = map.get("siteId") !=null ? map.get("siteId").toString() : "";
            String dafault = map.get("dafault") !=null ? map.get("dafault").toString() : "";
            CrmSiteReceivingInfo recevierInfos = new CrmSiteReceivingInfo();
            if(StringUtil.isNotEmpty(siteId)){
                recevierInfos.setSiteId(siteId);
                recevierInfos.setReleType(SourceConstants.ONE);
                if(SourceConstants.ZERO.equals(dafault)){
                    recevierAddressDao.setOtherAddress("",siteId);
                    recevierInfos.setDafaultAddress(SourceConstants.ZERO);
                }else{
                    recevierInfos.setDafaultAddress(SourceConstants.ONE);//不是默认选中地址
                }
            }else{
                User user = UserUtils.getUser();
                if(User.DEL_FLAG_AUDIT.equals(user.getUserType())){
                    recevierInfos.setReleType(SourceConstants.ZERO);
                }else{
                    recevierInfos.setSiteId(CrmUtils.getSiteId(user));
                    recevierInfos.setReleType(SourceConstants.ONE);
                }
                recevierInfos.setDafaultAddress(SourceConstants.ONE);//不是默认选中地址
            }
            recevierInfos.setReceiver(receiver);
            recevierInfos.setMobile(mobile);
            recevierInfos.setProvince(province);
            recevierInfos.setCity(city);
            recevierInfos.setArea(area);
            recevierInfos.setAddress(detailAddress);
            recevierInfos.setPostalCode(postCode);
            recevierInfos.setCreateTime(new Date());
            recevierInfos.setUpdateTime(new Date());
            recevierAddressDao.save(recevierInfos);
            mapRet.put("code","200");
            logger.info("数据保存成功:"+recevierInfos+",time:"+DateUtils.getDateTime());
        }catch (Exception e){
            e.printStackTrace();
            mapRet.put("code","205");
            logger.info("输出异常信息状态码:"+"205"+",输出异常信息:"+e);
        }
        return mapRet;
    }

    /**
     * 新增后删除操作
     * @param id
     * @return
     */
    public Result deleteBtn(String id){
        Result result = new Result();
        try {
            recevierAddressDao.deleteBtn(id);
            result.setCode("200");
            logger.info("数据删除成功！:"+recevierAddressDao+",time:"+DateUtils.getDateTime());
        }catch (Exception e){
           result.setCode("205");
            logger.info("数据删除失败！:"+e+",time:"+DateUtils.getDateTime());
        }
        return result;
    }

    /**
     * 根据id获取当前数据
     * @param id
     * @return
     */
    public Record getDataList(String id){
        return recevierAddressDao.getDataList(id);
    }

    /**
     * 修改中的保存
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object saveBtn(Map<String,String> map){
        Map<String,Object> mapRet = new HashMap<>();
        try{
            String id = map.get("id") !=null ? map.get("id").toString() : "";
            String receiver = map.get("receiver") !=null ? map.get("receiver").toString() : "";
            String mobile = map.get("mobile") !=null ? map.get("mobile").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 detailAddress = map.get("address") !=null ? map.get("address").toString() : "";
            String postCode = map.get("postcode") !=null ? map.get("postcode").toString() : "";
            if(StringUtils.isNotBlank(id)){
                CrmSiteReceivingInfo recevierInfo =  recevierAddressDao.get(id);
                logger.info("获取对象信息CrmFactoryReceivingInfo recevierInfo:"+recevierInfo+",time:"+ DateUtils.getDateTime());
                if(recevierInfo != null){
                    recevierInfo.setReceiver(receiver);
                    recevierInfo.setMobile(mobile);
                    recevierInfo.setProvince(province);
                    recevierInfo.setCity(city);
                    recevierInfo.setArea(area);
                    recevierInfo.setAddress(detailAddress);
                    recevierInfo.setPostalCode(postCode);
                    recevierAddressDao.save(recevierInfo);
                    mapRet.put("code","200");
                    logger.info("数据保存成功:"+recevierInfo+",time:"+DateUtils.getDateTime());
                }
            }else{
                User user = UserUtils.getUser();
                CrmSiteReceivingInfo recevierInfos = new CrmSiteReceivingInfo();
                if(User.DEL_FLAG_AUDIT.equals(user.getUserType())){
                    //厂家
                    recevierInfos.setReleType("0");
                }else{
                    String siteId = CrmUtils.getSiteId(user);
                    if(StringUtil.isBlank(siteId)){
                        siteId = map.get("siteId");
                    }
                    recevierInfos.setSiteId(siteId);
                    recevierInfos.setReleType("1");
                }
                recevierInfos.setReceiver(receiver);
                recevierInfos.setMobile(mobile);
                recevierInfos.setProvince(province);
                recevierInfos.setCity(city);
                recevierInfos.setArea(area);
                recevierInfos.setAddress(detailAddress);
                recevierInfos.setPostalCode(postCode);
                recevierAddressDao.save(recevierInfos);
                mapRet.put("code","200");
                logger.info("数据保存成功:"+recevierInfos+",time:"+DateUtils.getDateTime());
            }
        }catch (Exception e){
            e.printStackTrace();
            mapRet.put("code","205");
            logger.info("输出异常信息状态码:"+"205"+",输出异常信息:"+e);
        }
        return mapRet;
    }

    /**
     * 设置默认地址
     * @param id
     * @return
     */
    public Object setDafaultAddress(String id){
        Map<String,Object> mapRet = new HashMap<>();

            if(StringUtils.isNotBlank(id)){
               recevierAddressDao.setOtherAddress(id,"");//修改非默认地址状态为1
               recevierAddressDao.setDafaultAddress(id,"");//修改默认地址为状态0
                mapRet.put("code","200");
                logger.info("数据保存成功:"+recevierAddressDao+",time:"+DateUtils.getDateTime());
            }else{
                mapRet.put("code","205");
                logger.info("输出异常信息状态码:"+"205"+",输出异常信息:");
            }
        return mapRet;
    }

    /**
     * 服务商设置默认地址
     * @param id
     * @return
     */
    public Object SiteSetDafaultAddress(String id,String siteId){
        Map<String,Object> mapRet = new HashMap<>();
            if(StringUtils.isNotBlank(id)){
               recevierAddressDao.setOtherAddress(id,siteId);//修改非默认地址状态为1
               recevierAddressDao.setDafaultAddress(id,siteId);//修改默认地址为状态0
                mapRet.put("code","200");
            }else{
            mapRet.put("code","205");
            logger.info("输出异常信息状态码:"+"205"+",输出异常信息:");
        }
        return mapRet;
    }


    /**
     * 获取市
     */
    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);
    }


}
