package com.heyu.api.controller.mm;


import com.heyu.api.alibaba.request.mm.VvAdminCategoryRequest;
import com.heyu.api.alibaba.request.mm.VvAdminCategorySortRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvAdminCategoryDao;
import com.heyu.api.data.dao.vv.VvCategoryPropertyDao;
import com.heyu.api.data.dao.vv.VvCategoryPropertyValueDao;
import com.heyu.api.data.dto.vv.VvAdminCategoryCategoryDTO;
import com.heyu.api.data.dto.vv.VvCategoryPropertyDTO;
import com.heyu.api.data.entity.vv.VvAdminCategoryEntity;
import com.heyu.api.data.entity.vv.VvCategoryPropertyEntity;
import com.heyu.api.data.entity.vv.VvCategoryPropertyValueEntity;
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.util.CollectionUtils;
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/category")
public class AdminCategoryController {

    @Autowired
    private VvAdminCategoryDao vvAdminCategoryDao;


    @Autowired
    private VvCategoryPropertyValueDao vvCategoryPropertyValueDao;



    @Autowired
    private VvCategoryPropertyDao vvCategoryPropertyDao;
    /***
     * 列表
     *
     *
     * http://localhost:8888/mm/category/list
     *
     *
     */
    @Describe("后台类目列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvAdminCategoryRequest vvCategoryRequest) {
        List<VvAdminCategoryEntity> vvCategoryEntities = vvAdminCategoryDao.selectVvCategoryAll();
        List<VvAdminCategoryEntity> list = vvAdminCategoryDao.selectVvCategoryByParentId(vvCategoryRequest.getParentId());
        List<VvAdminCategoryCategoryDTO> vvCategoryDTOS = new ArrayList<>();
        for (VvAdminCategoryEntity vvCategoryEntity : list) {
            VvAdminCategoryCategoryDTO vvCategoryDTO = new VvAdminCategoryCategoryDTO();
            BeanUtils.copyProperties(vvCategoryEntity, vvCategoryDTO);
            boolean has = hasChild(vvCategoryEntity, vvCategoryEntities);
            vvCategoryDTO.setHasChild(has ? 1 : 0);

            // 如果没有子节点
            if (!has) {
                List<VvCategoryPropertyEntity> vvCategoryPropertyEntities = vvCategoryPropertyDao.selectVvPropertyByCategoryId(vvCategoryEntity.getId());

                if (!CollectionUtils.isEmpty(vvCategoryPropertyEntities)) {

                    List<VvCategoryPropertyDTO> vvCategoryPropertyDTOS = new ArrayList<>();
                    for (VvCategoryPropertyEntity vvCategoryPropertyEntity : vvCategoryPropertyEntities) {
                        VvCategoryPropertyDTO vvCategoryPropertyDTO = new VvCategoryPropertyDTO();
                        BeanUtils.copyProperties(vvCategoryPropertyEntity, vvCategoryPropertyDTO);


                        List<VvCategoryPropertyValueEntity> vvCategoryPropertyValueEntities = vvCategoryPropertyValueDao.selectVvPropertyValueByPropertyId(vvCategoryPropertyEntity.getId());
                        vvCategoryPropertyDTO.setVvPropertyValueList(vvCategoryPropertyValueEntities);

                        vvCategoryPropertyDTOS.add(vvCategoryPropertyDTO);
                    }
                    vvCategoryDTO.setVvCategoryPropertyDTOList(vvCategoryPropertyDTOS);
                }
            }
            vvCategoryDTOS.add(vvCategoryDTO);
        }
        return R.ok().setData(vvCategoryDTOS);
    }

    /***
     * 插入或更新
     * @param vvCategoryEntity
     * @return
     */
    @Describe("后台类目保存")
    @RequestMapping("/insertOrUpdate")
    public R insert(@RequestBody VvAdminCategoryEntity vvCategoryEntity) {
        vvAdminCategoryDao.insertOrUpdateVvAdminCategory(vvCategoryEntity);
        return R.ok().setData(vvCategoryEntity);
    }

    /***
     * 排序
     */
    @Describe("后台类目排序")
    @RequestMapping("/sort")
    public R sort(@RequestBody VvAdminCategorySortRequest vvCategorySortRequest) {
        List<VvAdminCategoryEntity> list = vvAdminCategoryDao.selectVvCategoryByIds(vvCategorySortRequest.getAdminCategoryIds());
        List<Long> categoryIds = vvCategorySortRequest.getAdminCategoryIds();
        for (VvAdminCategoryEntity vvCategoryEntity : list) {
            int index = categoryIds.indexOf(vvCategoryEntity.getId()) + 1;
            vvCategoryEntity.setDefaultSort(index);
            vvAdminCategoryDao.insertOrUpdateVvAdminCategory(vvCategoryEntity);
        }
        return R.ok().setData("保存成功");
    }



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

}
