package cn.pluss.platform.controller;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.MerchantImage;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchantImage.MerchantImageService;
import cn.pluss.platform.userApp.UserAppService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author DJH
 */
@RestController
@RequestMapping("/merchantImage")
public class MerchantImageController extends BaseModelController<MerchantImageService, MerchantImage> {

    @Setter(onMethod_ = {@Lazy, @Autowired})
    private UserAppService userAppService;

    /**
     * 上传图片
     */
    @PostMapping("saveBatch")
    @Transactional(rollbackFor = Exception.class)
    public Result<String> saveBatch(@RequestBody List<MerchantImage> merchantImageList) {
        UserApp userApp = userAppService.queryUserAppByToken();
        if (StringUtils.isEmpty(userApp.getMerchantCode())) {
            MsgException.throwException("缺少商户基本信息");
        }

        for (MerchantImage merchantImage: merchantImageList) {
            merchantImage.setMerchantCode(userApp.getMerchantCode());
        }

        baseService.saveMerchantImageBatch(merchantImageList);

        return ResultGenerator.genSuccessResult("保存成功", null);
    }
}

