package shop.alien.storeplatform.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreBusinessInfo;
import shop.alien.entity.store.vo.StoreBusinessInfoVo;
import shop.alien.mapper.StoreBusinessInfoMapper;
import shop.alien.storeplatform.service.StorePlatformBusinessInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 门店营业时间Controller
 *
 * @author ssk
 * @since 2024-12-05
 */
@Slf4j
@Api(tags = {"商户平台-门店营业信息"})
@ApiSort(2)
@CrossOrigin
@RestController
@RequestMapping("/storePlatformBusinessInfo")
@RequiredArgsConstructor
public class StorePlatformBusinessInfoController {

    private final StorePlatformBusinessInfoService storeBusinessInfoService;

    private final StoreBusinessInfoMapper storeBusinessInfoMapper;

    @ApiOperation("获取门店营业信息（包含正常营业时间和特殊营业时间，特殊营业时间关联节假日信息）")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/getByStoreId")
    public R<List<StoreBusinessInfoVo>> getByStoreId(Long storeId) {
        log.info("StorePlatformBusinessInfoController.getByStoreId?storeId={}", storeId);
        return R.data(storeBusinessInfoService.getStoreBusinessInfo(storeId));
    }

    @ApiOperation("新增或修改门店营业信息")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveOrUpdate")
    public R<Boolean> saveOrUpdate(@RequestBody StoreBusinessInfo storeBusinessInfo) {
        log.info("StorePlatformBusinessInfoController.order?storeBusinessInfo={}", storeBusinessInfo);
        if (storeBusinessInfoService.saveOrUpdate(storeBusinessInfo)) {
            if (null != storeBusinessInfo.getId()) {
                return R.success("修改成功");
            }
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("新增或修改门店营业信息批量保存")
    @ApiOperationSupport(order = 4)
    @PostMapping("/saveOrUpdateList")
    public R<Boolean> saveOrUpdateList(@RequestBody List<StoreBusinessInfo> businessInfoList) {
        log.info("StorePlatformBusinessInfoController.order?storeBusinessInfoVo={}", businessInfoList);
        boolean flag = false;
        List<StoreBusinessInfo> toSave = new ArrayList<>();
        List<StoreBusinessInfo> toUpdate = new ArrayList<>();
        for (StoreBusinessInfo businessInfo : businessInfoList) {
            if (businessInfo.getId() == null || businessInfo.getId() == 0) {
                toSave.add(businessInfo);
            } else {
                toUpdate.add(businessInfo);
            }
        }

        if (!toSave.isEmpty()) {
            //清除该店铺原始营业数据
            Optional<Integer> storeId = businessInfoList.stream().map(StoreBusinessInfo::getStoreId).findFirst();
            int value = storeId.orElse(0);
            LambdaUpdateWrapper<StoreBusinessInfo> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
            lambdaQueryWrapper.eq(StoreBusinessInfo::getStoreId,value);
            lambdaQueryWrapper.set(StoreBusinessInfo::getDeleteFlag,1);
            storeBusinessInfoMapper.update(null,lambdaQueryWrapper);
            flag = storeBusinessInfoService.saveBatch(toSave); // 批量保存
        }
        if(!toUpdate.isEmpty()){
            flag = storeBusinessInfoService.updateBatchById(toUpdate); // 批量更新
        }
        if(flag){
            return R.success("成功");
        }else{
            return R.fail("失败");
        }
    }

    @ApiOperation(value = "删除门店营业信息")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "Long", paramType = "query", required = true)
    })
    @GetMapping("/delete")
    public R<String> delete(Integer id) {
        log.info("StorePlatformBusinessInfoController.delete?ids={}", id);
        if (storeBusinessInfoService.removeById(id)) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }


}
