package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.VvCategoryPropertyRequest;
import com.heyu.api.alibaba.request.mm.VvCategoryPropertySortRequest;
import com.heyu.api.alibaba.request.mm.VvCategoryPropertyValueSortRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvCategoryPropertyDao;
import com.heyu.api.data.dao.vv.VvCategoryPropertyValueDao;
import com.heyu.api.data.dto.vv.VvCategoryPropertyDTO;
import com.heyu.api.data.entity.vv.VvCategoryPropertyEntity;
import com.heyu.api.data.entity.vv.VvCategoryPropertyValueEntity;
import com.heyu.api.data.utils.R;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
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/category/property")
public class AdminCategoryPropertyController {

    @Autowired
    private VvCategoryPropertyDao vvCategoryPropertyDao;


    @Autowired
    private VvCategoryPropertyValueDao vvCategoryPropertyValueDao;


    /***
     * 列表
     */
    @Describe("后台类目属性列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvCategoryPropertyRequest vvPropertyRequest) {

        PPageUtils pageUtils = PPageUtils.startPage(vvPropertyRequest.getPageNum(), vvPropertyRequest.getPageSize()).doSelect(new ISelect() {
            @Override
            public List doSelect(IPage page) {
                return vvCategoryPropertyDao.selectVvProperByCondition(page,
                        vvPropertyRequest.getAdminCategoryPropertyName(),
                        vvPropertyRequest.getAdminCategoryName(),
                        vvPropertyRequest.getAdminCategoryId());

            }
        });

        List<VvCategoryPropertyEntity> vvPropertyEntities = pageUtils.getRows();

        List<VvCategoryPropertyDTO> vvPropertyDTOS = new ArrayList<>();
        for (VvCategoryPropertyEntity vvPropertyEntity : vvPropertyEntities) {
            VvCategoryPropertyDTO vvPropertyDTO = new VvCategoryPropertyDTO();
            BeanUtils.copyProperties(vvPropertyEntity, vvPropertyDTO);
            List<VvCategoryPropertyValueEntity> vvPropertyValueEntityList = vvCategoryPropertyValueDao.selectVvPropertyValueByPropertyId(vvPropertyEntity.getId());
            vvPropertyDTO.setVvPropertyValueList(vvPropertyValueEntityList);
            vvPropertyDTOS.add(vvPropertyDTO);
        }

        pageUtils.setRows(vvPropertyDTOS);
        return R.ok().setData(pageUtils);
    }


    /***
     * 插入或更新
     *
     * /mm/category/property/insertOrUpdate
     */
    @Describe("后台类目属性保存")
    @RequestMapping("/insertOrUpdate")
    public R insert(@RequestBody VvCategoryPropertyEntity vvCategoryProperty) {
        vvCategoryPropertyDao.insertOrUpdateVvCategoryProperty(vvCategoryProperty);
        return R.ok().setData(vvCategoryProperty);
    }


    /***
     * 插入或更新
     *
     * /mm/category/property/delete
     */
    @Describe("后台类目属性删除")
    @RequestMapping("/delete")
    public R delete(@RequestBody VvCategoryPropertyEntity vvCategoryProperty) {
        vvCategoryPropertyDao.deleteVvCategoryPropertyById(vvCategoryProperty.getId());
        return R.ok();
    }


    /***
     * 插入或更新
     *
     * /mm/category/property/value/insertOrUpdate
     */
    @Describe("后台类目属性插入或更新")
    @RequestMapping("/value/insertOrUpdate")
    public R insert(@RequestBody VvCategoryPropertyValueEntity vvCategoryPropertyValueEntity) {
        vvCategoryPropertyValueDao.insertOrUpdateVvCategoryPropertyValue(vvCategoryPropertyValueEntity);
        return R.ok().setData(vvCategoryPropertyValueEntity);
    }



    /***
     * 插入或更新
     *
     * /mm/category/property/value/delete
     */
    @Describe("后台类目属性删除")
    @RequestMapping("/value/delete")
    public R delete(@RequestBody VvCategoryPropertyValueEntity vvCategoryProperty) {
        vvCategoryPropertyValueDao.deleteVvCategoryPropertyValueById(vvCategoryProperty.getId());
        return R.ok();
    }


    /***
     * 排序
     */
    @Describe("后台类目属性排序")
    @RequestMapping("/sort")
    public R sort(@RequestBody VvCategoryPropertySortRequest vvPropertySortRequest) {
        List<VvCategoryPropertyEntity> propertyEntities = vvCategoryPropertyDao.selectVvPropertyByIds(vvPropertySortRequest.getCategoryPropertyIds());
        for (VvCategoryPropertyEntity propertyEntity : propertyEntities) {
            propertyEntity.setDefaultSort(vvPropertySortRequest.getCategoryPropertyIds().indexOf(propertyEntity.getId()) + 1);
            vvCategoryPropertyDao.insertOrUpdateVvCategoryProperty(propertyEntity);
        }
        return R.ok().setData("保存成功");
    }


    /***
     * 排序
     */
    @Describe("后台类目属性排序index")
    @RequestMapping("/value/sort")
    public R sortValue(@RequestBody VvCategoryPropertyValueSortRequest vvPropertyValueSortRequest) {
        List<VvCategoryPropertyValueEntity> propertyEntities = vvCategoryPropertyValueDao.selectVvPropertyValueByIds(vvPropertyValueSortRequest.getCategoryPropertyValueIds());
        for (VvCategoryPropertyValueEntity propertyEntity : propertyEntities) {
            propertyEntity.setDefaultSort(vvPropertyValueSortRequest.getCategoryPropertyValueIds().indexOf(propertyEntity.getId()) + 1);
            vvCategoryPropertyValueDao.insertOrUpdateVvCategoryPropertyValue(propertyEntity);
        }
        return R.ok().setData("保存成功");
    }



}
