package cn.iocoder.yudao.module.member.controller.app.store;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.StoreResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.store.dto.StoreBaseDto;
import cn.iocoder.yudao.module.member.controller.app.store.dto.StoreSaleGoodsDto;
import cn.iocoder.yudao.module.member.controller.app.store.dto.StoreUserDto;
import cn.iocoder.yudao.module.member.controller.app.store.vo.StoreGoodsVo;
import cn.iocoder.yudao.module.member.controller.app.store.vo.StoreUserVo;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.storegoods.StoreGoodsService;
import cn.iocoder.yudao.module.member.service.storegoodstype.StoreGoodsTypeService;
import cn.iocoder.yudao.module.member.service.storesalegoods.StoreSaleGoodsService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import java.util.List;

@Tag(name = "用户 APP - 超市")
@RestController
@RequestMapping("/device")
@Validated
@Slf4j
public class AppStoreController {

    @Resource
    private MemberUserService userService;
    @Resource
    private StoreGoodsService storeGoodsService;
    @Resource
    private StoreGoodsTypeService goodsTypeService;
    @Resource
    private StoreSaleGoodsService saleGoodsService;

    @GetMapping("/mgcr/memberUser/getMenberTwoList")
    @Operation(summary = "获取用户关联信息")
    public StoreResult<List<StoreUserVo>> getStoreUser(@RequestBody StoreUserDto dto) {
        List<MemberUserDO> list = userService.getStoreUser();
        return StoreResult.success(StoreUserVo.convert(list));
    }

    @GetMapping("/mgcr/equipment/goodsList")
    @Operation(summary = "获取菜品库所有菜品")
    public StoreResult<StoreGoodsVo> getGoods(@RequestBody StoreBaseDto dto) {
        StoreGoodsVo storeGoodsVo = new StoreGoodsVo();
        storeGoodsVo.setGoodsData(storeGoodsService.getAll(null));
        storeGoodsVo.setCategoryData(goodsTypeService.getAll());
        return StoreResult.success(storeGoodsVo);
    }

    @PostMapping("/mgcr/equipment/bindGoods")
    @Operation(summary = "设置营业菜品")
    public StoreResult<String> bindGoods(@RequestBody StoreSaleGoodsDto dto) {
        Boolean b = saleGoodsService.bindGoods(dto);
        StoreResult<String> result = new StoreResult<>();

        if (b) {
            result.setStatusCode("200");
            result.setMessage("操作成功");
            result.setSuccess(true);
        }else{
            result.setStatusCode("300");
            result.setSuccess(false);
        }
        return result;
    }

    @GetMapping("/mgcr/equipment/queryBindGoods")
    @Operation(summary = "获取营业菜品")
    public StoreResult<StoreGoodsVo> getSaleGoods(@RequestBody StoreBaseDto dto) {
        StoreGoodsVo storeGoodsVo = new StoreGoodsVo();

        List<Integer> goodsIds = saleGoodsService.getGoodsIds(dto.getEquipmentCode());
        if(CollectionUtil.isEmpty(goodsIds)){
            return StoreResult.success(storeGoodsVo);
        }
        storeGoodsVo.setGoodsData(storeGoodsService.getAll(goodsIds));
        storeGoodsVo.setCategoryData(goodsTypeService.getAll());
        return StoreResult.success(storeGoodsVo);
    }


}
