package com.fzm.mall.server.admin.goods_center.controller;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.admin.enums.setting.GoodsConfigureEnum;
import com.fzm.mall.server.admin.general_model.service.IPlatformSettingService;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.model.Merchant;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.home_page.service.IMerchantService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wangp
 * @date 2021/8/17 10:58
 * @description 商品配置
 * @since JDK 1.8
 */
@Api(tags = "商品中心-商品配置")
@RestController
@RequestMapping("/goodsConfigure")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsConfigureController {

    private final IPlatformSettingService platformSettingService;
    private final IMerchantService merchantService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "平台详情")
    @GetMapping("/platDetail")
    public ResponseVO platDetail() {
        String result = platformSettingService.queryByEvent("user_delay_delivery");
        return resFac.getSuccessResponseWithData(Integer.parseInt(result));
    }

    @ApiOperation(value = "商户详情")
    @GetMapping("/merchantDetail")
    public ResponseVO merchantDetail(@RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        String result = platformSettingService.queryByEvent("user_delay_delivery");
        JSONObject json = new JSONObject();
        json.put("platform", result);
        if (Integer.valueOf(result).equals(GoodsConfigureEnum.IMMEDIATE_DELIVERY.status())) {
            json.put("isDelivery", GoodsConfigureEnum.IMMEDIATE_DELIVERY.status());
        } else {
            Merchant merchant = merchantService.getByMerchantId(admin.getMerchantId());
            json.put("isDelivery", merchant.getDelayDelivery());
        }
        return resFac.getSuccessResponseWithData(json);
    }

    @ApiOperation(value = "平台配置")
    @PutMapping("/platConfigure")
    public ResponseVO platConfigure(@ApiParam(name = "delayDelivery", value = "是否延后提货(0.否 1.是)") @RequestParam Integer delayDelivery) {
        platformSettingService.delayDelivery(delayDelivery.toString());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "商户配置")
    @PutMapping("/merchantConfigure")
    public ResponseVO merchantConfigure(@ApiParam(name = "delayDelivery", value = "是否延后提货(0.否 1.是)") @RequestParam Integer delayDelivery,
                                         @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        merchantService.delayDelivery(delayDelivery, admin.getMerchantId());
        return resFac.getSimpleSuccessResponse();
    }

}
