/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.Area;
import com.yami.shop.bean.model.Delivery;
import com.yami.shop.delivery.comment.api.paotui.PaoTuiApi;
import com.yami.shop.delivery.comment.api.paotui.model.response.City;
import com.yami.shop.delivery.comment.api.paotui.model.response.District;
import com.yami.shop.service.AreaService;
import com.yami.shop.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lgh on 2018/11/26.
 */
@RestController
@RequestMapping("/admin/delivery")
public class DeliveryController {

    @Autowired
    private DeliveryService deliveryService;

    @Autowired
    private PaoTuiApi paoTuiApi;

    @Autowired
    private AreaService areaService;


    /**
     * 分页获取
     */
    @GetMapping("/list")
    public ResponseEntity<List<Delivery>> page() {

        List<Delivery> list = deliveryService.list();
        return ResponseEntity.ok(list);
    }

    @GetMapping("/configCity")
    public ResponseEntity<List<City>> configCity() {
        List<City> city = paoTuiApi.configCity();
        city.forEach(it -> {

            List<Area> list = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getCode, it.getP_code()));
            if (list.size() == 0) {
                Area parea = new Area();
                parea.setCode(it.getP_code());
                parea.setAreaName(it.getP_name());
                parea.setParentId(0L);
                parea.setLevel(1);
                areaService.save(parea);
            }

            List<Area> lists = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getCode, it.getCode()));

            if (lists.size() == 0) {
                Area area = new Area();
                area.setAreaName(it.getName());
                area.setCode(it.getCode());
                area.setParentId(it.getP_code());
                area.setLevel(2);
                areaService.save(area);
            }
        });
        return ResponseEntity.ok(city);
    }


    @GetMapping("/configDistrict")
    public ResponseEntity<List<District>> configDistrict() {
        List<District> districts = paoTuiApi.configDistrict();
        districts.forEach(it -> {

            List<Area> lists = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getCode, it.getCode()));
            if (lists.size() == 0) {

                Area area = new Area();
                area.setAreaName(it.getName());
                area.setCode(it.getCode());
                area.setParentId(it.getP_code());
                area.setLevel(3);
                areaService.save(area);
            }

        });
        return ResponseEntity.ok(districts);
    }

}
