package cn.iocoder.yudao.module.member.controller.admin.storegoodsinventory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.storegoodsinventory.vo.AddReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storegoodsinventory.vo.StoreGoodsInventoryPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storegoodsinventory.vo.StoreGoodsInventoryRespVO;
import cn.iocoder.yudao.module.member.controller.admin.storegoodsinventory.vo.StoreGoodsInventorySaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoods.StoreGoodsDO;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoodsinventory.StoreGoodsInventoryDO;
import cn.iocoder.yudao.module.member.service.async.MemberAsyncService;
import cn.iocoder.yudao.module.member.service.storegoods.StoreGoodsService;
import cn.iocoder.yudao.module.member.service.storegoodsinventory.StoreGoodsInventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 商品库存")
@RestController
@RequestMapping("/member/store-goods-inventory")
@Validated
public class StoreGoodsInventoryController {

    @Resource
    private StoreGoodsInventoryService storeGoodsInventoryService;
    @Resource
    private StoreGoodsService storeGoodsService;
    @Resource
    private MemberAsyncService memberAsyncService;


    @PostMapping("/create")
    @Operation(summary = "出入库")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory:create')")
    public CommonResult<Integer> createStoreGoodsInventory(@Valid @RequestBody AddReqVO createReqVO) {
        Integer storeGoodsInventory = storeGoodsInventoryService.createStoreGoodsInventory(createReqVO);
        memberAsyncService.batchRecord(createReqVO);
        return success(storeGoodsInventory);
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品库存")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory:update')")
    public CommonResult<Boolean> updateStoreGoodsInventory(@Valid @RequestBody StoreGoodsInventorySaveReqVO updateReqVO) {
        storeGoodsInventoryService.updateStoreGoodsInventory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品库存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory:delete')")
    public CommonResult<Boolean> deleteStoreGoodsInventory(@RequestParam("id") Integer id) {
        storeGoodsInventoryService.deleteStoreGoodsInventory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory:query')")
    public CommonResult<StoreGoodsInventoryRespVO> getStoreGoodsInventory(@RequestParam("id") Integer id) {
        StoreGoodsInventoryDO storeGoodsInventory = storeGoodsInventoryService.getStoreGoodsInventory(id);
        StoreGoodsInventoryRespVO bean = BeanUtils.toBean(storeGoodsInventory, StoreGoodsInventoryRespVO.class);
        StoreGoodsDO storeGoods = storeGoodsService.getStoreGoods(storeGoodsInventory.getGoodsId());
        BeanUtil.copyProperties(storeGoods, bean);
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品库存分页")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory:query')")
    public CommonResult<PageResult<StoreGoodsInventoryRespVO>> getStoreGoodsInventoryPage(@Valid StoreGoodsInventoryPageReqVO pageReqVO) {
        PageResult<StoreGoodsInventoryDO> pageResult = storeGoodsInventoryService.getStoreGoodsInventoryPage(pageReqVO);
        PageResult<StoreGoodsInventoryRespVO> bean = BeanUtils.toBean(pageResult, StoreGoodsInventoryRespVO.class);
        List<StoreGoodsInventoryRespVO> list = bean.getList();
        if(CollectionUtil.isNotEmpty(list)){
            List<Integer> goodsList = list.stream().map(StoreGoodsInventoryRespVO::getGoodsId).collect(Collectors.toList());
            List<StoreGoodsDO> all = storeGoodsService.getAll(goodsList);
            Map<Integer, StoreGoodsDO> collect = all.stream().collect(Collectors.toMap(StoreGoodsDO::getGoodsId, a -> a));
            for (StoreGoodsInventoryRespVO vo:list){
                BeanUtil.copyProperties(collect.get(vo.getGoodsId()), vo);
            }

        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品库存 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreGoodsInventoryExcel(@Valid StoreGoodsInventoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreGoodsInventoryDO> list = storeGoodsInventoryService.getStoreGoodsInventoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品库存.xls", "数据", StoreGoodsInventoryRespVO.class,
                        BeanUtils.toBean(list, StoreGoodsInventoryRespVO.class));
    }

}