package com.slodon.b2b2c.controller.seller.seller;

import com.slodon.b2b2c.aop.VendorLogger;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.model.seller.StoreExpressSettingModel;
import com.slodon.b2b2c.seller.example.StoreExpressSettingExample;
import com.slodon.b2b2c.seller.pojo.StoreExpressSetting;
import com.slodon.b2b2c.seller.pojo.Vendor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author slodon
 */
@Api(tags = "seller-快递鸟设置")
@Slf4j
@RestController
@RequestMapping("v3/seller/seller/express/setting")
public class SellerExpressSettingController extends BaseController {

    @Resource
    private StoreExpressSettingModel storeExpressSettingModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("快递鸟设置详情")
    @GetMapping("detail")
    public JsonResult<StoreExpressSetting> detail(HttpServletRequest request) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        StoreExpressSettingExample example = new StoreExpressSettingExample();
        example.setStoreId(vendor.getStoreId());
        List<StoreExpressSetting> list = storeExpressSettingModel.getStoreExpressSettingList(example, null);
        StoreExpressSetting storeExpressSetting = new StoreExpressSetting();
        if (!CollectionUtils.isEmpty(list)) {
            storeExpressSetting = list.get(0);
        }
        String accountSetting = stringRedisTemplate.opsForValue().get("express_account_setting");
        if (StringUtil.isEmpty(accountSetting) || "1".equals(accountSetting)) {
            storeExpressSetting.setIsHidden(1);
        } else {
            storeExpressSetting.setIsHidden(0);
        }
        return SldResponse.success(storeExpressSetting);
    }

    @ApiOperation("保存快递鸟设置")
    @VendorLogger(option = "保存快递鸟设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "businessId", value = "快递鸟电商ID"),
            @ApiImplicitParam(name = "appKey", value = "加密私钥"),
            @ApiImplicitParam(name = "printerName", value = "打印机名称", required = true)
    })
    @PostMapping("save")
    public JsonResult<Integer> addExpress(HttpServletRequest request, String businessId, String appKey, String printerName) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        StoreExpressSetting storeExpressSetting = new StoreExpressSetting();
        storeExpressSetting.setStoreId(vendor.getStoreId());
        storeExpressSetting.setBusinessId(businessId);
        storeExpressSetting.setAppKey(appKey);
        storeExpressSetting.setPrinterName(printerName);
        storeExpressSettingModel.updateStoreExpressSetting(storeExpressSetting);
        return SldResponse.success("保存成功", "店铺id:" + vendor.getStoreId());
    }

}
