package com.jzdsh.auto.biz;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jzdsh.auto.model.*;
import com.jzdsh.util.Api;
import com.jzdsh.util.CommonCons;
import com.jzdsh.util.CommonUtil;
import com.jzdsh.util.MapUtils;
import com.jzdsh.vo.HomepageVo;
import com.jzdsh.vo.UserStatus;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @ProjectName: auto
 * @Package: com.jzdsh.auto.biz
 * @ClassName: HomepageService
 * @Author: Jzdsh
 * @Description: 主页
 * @Date: 2020/3/21 10:59
 * @Version: 1.0
 */
@Log4j2
public class HomepageService {

    @Inject
    NoticeService noticeService;

    @Inject
    UserInfoService userInfoService;

    @Inject
    ServiceCategoryService categoryService;

    @Inject
    UdaService udaService;

    @Inject
    OwnerService ownerService;

    @Inject
    ShopService shopService;

    @Inject
    CouponService couponService;

    @Inject
    CouponSetService couponSetService;

    @Inject
    SearchProviderService searchProviderService;

    @Inject
    SearchDealerService searchDealerService;

    @Inject
    BrandFourSService brandFourSService;

    @Inject
    CarCommercialService carCommercialService;

    @Inject
    CarSendanService carSendanService;


    @Before(Tx.class)
    public Api show(String token, String longitude, String latitude, int pageNum) {
        HomepageVo vo = new HomepageVo( );

        try {
            if (StringUtils.isBlank(longitude) || Objects.equals(CommonCons.UNDEFINED, longitude)) {
                longitude = "0";
                latitude = "0";
            }

            //-- 获取当前地理信息 区域
            Map< String, String > map = MapUtils.getMap(longitude, latitude);
            String province = map.get("province");
            String city = map.get("city");
            String district = map.get("district");
            if (CommonCons.BRACKETS.equals(city)) {
                city = province;
            }
            //城市
            vo.setDistrict(district);
            //存在token情况下
            haveToken(vo, token, longitude, latitude, province, city, district);
            //层级
            haveLevel(vo);
            //--车主服务
            vo.setOwner(ownerService.getByIsRecommend( ));
            //--

            //商家信息
            List< Shop > shop = shopService.findBySearchName(longitude, latitude, pageNum, CommonCons.PAGE_SIZE);

            shop.forEach(sp -> {
                String pMainBusiness = sp.getPMainBusiness( );
                if (pMainBusiness != null) {
                    List< String > l = Arrays.asList(pMainBusiness.split(","));
                    if (l.size( ) > 3) {
                        List< String > list = l.subList(0, 3);
                        sp.set("pMainBusiness", list);
                    } else {
                        sp.set("pMainBusiness", l);
                    }
                }

                String format = String.format("%.0f", sp.getDouble("distance") * 1000);
                Float distance = Float.valueOf(format);
                if (distance >= 1000) {
                    sp.put("distance", "距您" + String.format("%.1f", distance / 1000) + "km");
                } else {
                    sp.put("distance", "距您" + String.format("%.0f", distance) + "m");
                }
            });
            vo.setShop(shop);
            //return Ret.ok("data", Api.ok(vo));
            return Api.ok(vo);
        } catch (Exception e) {
            log.info("e", e);
        }
        return Api.fail(null);
    }

    private void haveLevel(HomepageVo vo) {
        //层级
        List< ServiceCategory > levelOne = categoryService.getLeve(CommonCons.ROLE_ONE, CommonCons.LEVEL_ONE);
        vo.setServiceCategoryOne(levelOne);
        List< ServiceCategory > levelOnes = new ArrayList<>( );
        levelOnes.addAll(levelOne);
        ServiceCategory serviceCategory = new ServiceCategory( );
        serviceCategory.setId(0);
        serviceCategory.setName("推荐");
        serviceCategory.setPId(0L);
        levelOnes.add(0, serviceCategory);
        vo.setServiceCategoryOnes(levelOnes);
        //第二层
        List< ServiceCategory > levelTwo = categoryService.getLevelShow(CommonCons.ROLE_ONE, CommonCons.LEVEL_TWO, 9);
        levelTwo.forEach(o -> {
            o.put("ids", "y" + o.getId( ));

        });
        vo.setServiceCategoryTwo(levelTwo);
        //
    }


    private void haveToken(HomepageVo vo, String token, String longitude, String latitude, String province, String city, String district) {
        try {
            if (StringUtils.isNotBlank(token)) {
                UserInfo userInfo = userInfoService.getByToken(token);
                userInfo.setLongitude(longitude);
                userInfo.setLatitude(latitude);
                userInfo.update( );
                //返回用户 认证信息
                vo.setRoleType(userInfo.getRole( ));
                //返回用户认证状态
                UserStatus userStatus = userInfoService.getUserStatus(userInfo.getId( ));
                vo.setUserStatus(userStatus);
                //end
                Uda udaInfo;
                udaInfo = udaService.getByUserId(userInfo.getId( ));
                if (udaInfo == null) {
                    //证明是第一次进去该系统
                    Uda uda = new Uda( );
                    uda.setUserId(userInfo.getId( ));
                    uda.setProvince(province);
                    uda.setCity(city);
                    uda.setDistrict(district);
                    uda.save( );
                } else {
                    province = udaInfo.getProvince( );
                    city = udaInfo.getCity( );
                    district = udaInfo.getDistrict( );
                }
                // 用户修改
                {
                    HomepageVo.InnerCity innerCity = vo.new InnerCity( );
                    if (udaInfo.getCity( ).equals(city) || city.equals(userInfo.getNewCity( ))) {
                        innerCity.setBoolen(true);
                        innerCity.setNewCity(city);
                        innerCity.setOldCity(udaInfo.getCity( ));
                    } else {
                        innerCity.setBoolen(false);
                        innerCity.setNewCity(city);
                        innerCity.setOldCity(udaInfo.getCity( ));
                        innerCity.setTips("定位到您在 ♢" + city + "是否切换到该城市进行探索?");
                    }
                    vo.setInnerCity(innerCity);
                }
            }
            //---公告
            List< Notice > notices = noticeService.listByProvinceCityPageSize(province, city, 10);
            vo.setNotice(notices);
        } catch (Exception e) {
            log.info("子类show" + e);
        }
    }





}
