package com.zbkj.admin.controller;

import com.zbkj.common.model.home.HomeItem;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.HomeItemRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.vo.HomeItemVo;
import com.zbkj.service.service.EbHomeItemService;
import com.zbkj.service.service.StoreProductService;
import com.zbkj.service.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("api/admin/floorItem/")
@Api(tags = "楼层商品")
public class HomeItemController {

    @Autowired
    private EbHomeItemService homeItemService;
    @Autowired
    private SystemAttachmentService systemAttachmentService;
    @Autowired
    private StoreProductService storeProductService;

    @PreAuthorize("hasAuthority('admin:homeItem:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<HomeItemVo>> getList(@Validated HomeItemRequest request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(homeItemService.getAdminList(request, pageParamRequest)));
    }

    @PreAuthorize("hasAuthority('admin:homeItem:save')")
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated List<HomeItemRequest> itemList) {
        for (HomeItemRequest req : itemList) {
            HomeItem add = new HomeItem();
            BeanUtils.copyProperties(req, add);
            add.setId(null);
            add.setType(add.getJumpType() == 1 || add.getJumpType() == 3 ? 0 : 1);
            add.setImgUrl(systemAttachmentService.clearPrefix(add.getImgUrl()));
            homeItemService.save(add);
        }
        return CommonResult.success();
    }

    @PreAuthorize("hasAuthority('admin:homeItem:delete')")
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult<String> delete(@RequestParam(value = "ids") String ids) {
        List<Integer> idList = Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        if (homeItemService.removeByIds(idList)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:homeItem:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody HomeItemRequest request) {
        HomeItem upd = new HomeItem();
        BeanUtils.copyProperties(request, upd);
        upd.setImgUrl(systemAttachmentService.clearPrefix(upd.getImgUrl()));
        if (homeItemService.updateById(upd)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:homeItem:info')")
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<HomeItemVo> info(@PathVariable Integer id) {
        HomeItem homeItem = homeItemService.getById(id);
        HomeItemVo homeItemVo = new HomeItemVo();
        BeanUtils.copyProperties(homeItem, homeItemVo);
        if (homeItemVo.getJumpType() == 1 || homeItemVo.getJumpType() == 3) {
            String[] split = homeItemVo.getJumpIds().split(",");
            List<Integer> pIds = Arrays.asList(split).stream().map(i -> Integer.parseInt(i)).collect(Collectors.toList());
            homeItemVo.setJumpIdsText(storeProductService.getName(pIds).values().stream().collect(Collectors.joining(",")));
        }
        return CommonResult.success(homeItemVo);
   }
}



