package com.heyu.api.controller.vv;


import com.heyu.api.alibaba.request.mm.VvAppCategoryRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvAppCategoryDao;
import com.heyu.api.data.dto.vv.VvAppCategoryCategoryDTO;
import com.heyu.api.data.entity.vv.VvAppCategoryEntity;
import com.heyu.api.data.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app/category")
public class AppCategoryController {

    @Autowired
    private VvAppCategoryDao vvAppCategoryDao;

    /***
     * 列表
     *
     *
     * http://localhost:8888/app/category/list
     */
    @RequestMapping("/list")
    @Describe("app类目列表")
    public R list(@RequestBody VvAppCategoryRequest vvCategoryRequest) {
        List<VvAppCategoryEntity> vvCategoryEntities = vvAppCategoryDao.selectVvCategoryAll();

        List<VvAppCategoryEntity> list = vvAppCategoryDao.selectVvCategoryByParentId(0);


        List<VvAppCategoryCategoryDTO> vvCategoryDTOS = new ArrayList<>();
        for (VvAppCategoryEntity vvAppCategoryEntity : list) {

            VvAppCategoryCategoryDTO vvAppCategoryCategoryDTO = new VvAppCategoryCategoryDTO();
            BeanUtils.copyProperties(vvAppCategoryEntity, vvAppCategoryCategoryDTO);

            for (VvAppCategoryEntity vvCategoryEntity : vvCategoryEntities) {

                if(vvCategoryEntity.getParentId().equals(vvAppCategoryEntity.getId())){

                    vvAppCategoryCategoryDTO.getChildrens().add(vvCategoryEntity);
                }
            }
            vvCategoryDTOS.add(vvAppCategoryCategoryDTO);
        }


        return R.ok().setData(vvCategoryDTOS);
    }


}
