package com.heyu.api.controller.mm;


import com.heyu.api.alibaba.request.mm.VvAppCategoryIndexSortRequest;
import com.heyu.api.alibaba.request.mm.VvAppCategoryRequest;
import com.heyu.api.alibaba.request.mm.VvAppCategorySortRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvAppCategoryDao;
import com.heyu.api.data.dto.vv.VvAdminCategoryCategoryDTO;
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("/mm/app/category")
public class AdminAppCategoryController {

    @Autowired
    private VvAppCategoryDao vvAppCategoryDao;

    /***
     * 列表
     *
     *
     * http://localhost:8888/mm/category/list
     */
    @Describe("app类目列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvAppCategoryRequest vvCategoryRequest) {
        List<VvAppCategoryEntity> vvCategoryEntities = vvAppCategoryDao.selectVvCategoryAll();
        List<VvAppCategoryEntity> list = vvAppCategoryDao.selectVvCategoryByParentId(vvCategoryRequest.getParentId());
        List<VvAdminCategoryCategoryDTO> vvCategoryDTOS = new ArrayList<>();
        for (VvAppCategoryEntity vvCategoryEntity : list) {
            VvAdminCategoryCategoryDTO vvCategoryDTO = new VvAdminCategoryCategoryDTO();
            BeanUtils.copyProperties(vvCategoryEntity, vvCategoryDTO);
            boolean has = hasChild(vvCategoryEntity, vvCategoryEntities);
            vvCategoryDTO.setHasChild(has ? 1 : 0);
            vvCategoryDTOS.add(vvCategoryDTO);
        }
        return R.ok().setData(vvCategoryDTOS);
    }


    /***
     * 插入或更新
     * @param vvCategoryEntity
     * @return
     */
    @Describe("app类目保存")
    @RequestMapping("/insertOrUpdate")
    public R insert(@RequestBody VvAppCategoryEntity vvCategoryEntity) {
        vvAppCategoryDao.insertOrUpdateVvAppCategory(vvCategoryEntity);
        return R.ok().setData(vvCategoryEntity);
    }


    /***
     * 排序
     */
    @Describe("app类目排序")
    @RequestMapping("/sort")
    public R sort(@RequestBody VvAppCategorySortRequest vvCategorySortRequest) {
        List<VvAppCategoryEntity> list = vvAppCategoryDao.selectVvCategoryByIds(vvCategorySortRequest.getAppCategoryIds());
        List<Long> categoryIds = vvCategorySortRequest.getAppCategoryIds();
        for (VvAppCategoryEntity vvCategoryEntity : list) {
            int index = categoryIds.indexOf(vvCategoryEntity.getId()) + 1;
            vvCategoryEntity.setDefaultSort(index);
            vvAppCategoryDao.insertOrUpdateVvAppCategory(vvCategoryEntity);
        }
        return R.ok().setData("保存成功");
    }


    /***
     * 排序
     */
    @Describe("app类目排序index")
    @RequestMapping("/update/sort/index")
    public R updateSortIndex(@RequestBody VvAppCategoryIndexSortRequest vvCategorySortRequest) {
        VvAppCategoryEntity vvCategory = vvAppCategoryDao.selectVvAppCategoryById(vvCategorySortRequest.getAppCategoryId());
        vvCategory.setDefaultSort(vvCategorySortRequest.getDefaultSort());
        vvAppCategoryDao.insertOrUpdateVvAppCategory(vvCategory);
        return R.ok().setData("保存成功");
    }


    public boolean hasChild(VvAppCategoryEntity vvCategoryEntity, List<VvAppCategoryEntity> vvCategoryEntities) {
        for (VvAppCategoryEntity categoryEntity : vvCategoryEntities) {
            if (vvCategoryEntity.getId().equals(categoryEntity.getParentId())) {
                return true;
            }
        }
        return false;
    }

}
