package shop.alien.store.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.store.LifeBrandedCard;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.store.service.LifeBrandedCardService;
import shop.alien.util.common.FileUpload;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联名卡
 */
@Api(tags = {"一期-联名卡"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/brandedcard")
@RequiredArgsConstructor
public class LifeBrandedCardController {

    private final LifeBrandedCardService lifeBrandedCardService;

    private final FileUpload fileUpload;

    @PostMapping("addOrUpdateBrandedCard")
    public Map<String, Object> addOrUpdateBrandedCard(LifeBrandedCard card,
                                                      @RequestParam(value = "image1", required = false) MultipartFile image1,
                                                      @RequestParam(value = "image2", required = false) MultipartFile image2,
                                                      @RequestParam(value = "image3", required = false) MultipartFile image3,
                                                      @RequestParam(value = "image4", required = false) MultipartFile image4,
                                                      @RequestParam(value = "image5", required = false) MultipartFile image5) {
        log.info("LifeBrandedCardController.addOrUpdateBrandedCard?image1={}&image2={}&image3={}&image4={}&image5={}", image1.getOriginalFilename(), image2.getOriginalFilename(), image3.getOriginalFilename(), image4.getOriginalFilename(), image5.getOriginalFilename());
        Map<String, Object> response = new HashMap<>();
        try {
            String path = "";
            // 保存图片
            if (image1 != null && !StringUtils.isEmpty(image1.getOriginalFilename())) {
                String imagePath = fileUpload.saveImage(image1);
                path += imagePath + ";";
            }
            // 保存图片
            if (image2 != null && !StringUtils.isEmpty(image2.getOriginalFilename())) {
                String imagePath = fileUpload.saveImage(image2);
                path += imagePath + ";";
            }
            // 保存图片
            if (image3 != null && !StringUtils.isEmpty(image3.getOriginalFilename())) {
                String imagePath = fileUpload.saveImage(image3);
                path += imagePath + ";";
            }
            // 保存图片
            if (image4 != null && !StringUtils.isEmpty(image4.getOriginalFilename())) {
                String imagePath = fileUpload.saveImage(image4);
                path += imagePath + ";";
            }
            // 保存图片
            if (image5 != null && !StringUtils.isEmpty(image5.getOriginalFilename())) {
                String imagePath = fileUpload.saveImage(image5);
                path += imagePath + ";";
            }
            // 设置图片路径
            card.setImages(path);
            // 保存到数据库
            int cnt = lifeBrandedCardService.addOrUpdateBrandedCard(card);
            response.put("success", cnt);
        } catch (Exception e) {
            log.error("LifeBrandedCardController.addOrUpdateBrandedCard ERROR Msg={}", e.getMessage());
            response.put("result", "失败");
            return response;
        }
        return response;
    }

    @GetMapping("getStoreList")
    private List<StoreInfoVo> getStoreList(@RequestParam("storeName") String storeName) {
        log.info("LifeBrandedCardController.getStoreList?storeName={}", storeName);
        return lifeBrandedCardService.getStoreList(storeName);
    }

    @GetMapping("getQuanListByStoreId")
    private List<LifeCoupon> getQuanListByStoreId(@RequestParam("storeId") List<String> storeIds) {
        log.info("LifeBrandedCardController.getQuanListByStoreId?storeId={}", storeIds);
        return lifeBrandedCardService.getQuanListByStoreId(storeIds);
    }

    @GetMapping("getBrandedCardDetail")
    private Map<String, Object> getBrandedCardDetail(@RequestParam("cardId") String cardId) {
        log.info("LifeBrandedCardController.getBrandedCardDetail?cardId={}", cardId);
        return lifeBrandedCardService.getBrandedCardDetail(cardId);
    }

    @PostMapping("updateCardStatus")
    public int updateCardStatus(@RequestParam("id") String id, @RequestParam("status") String status) {
        log.info("LifeBrandedCardController.updateCardStatus?id={}&status={}", id, status);
        return lifeBrandedCardService.updateCardStatus(id, status);
    }

    @GetMapping("getBrandedCardList")
    private Map<String, Object> getBrandedCardList(@RequestParam(value = "page", defaultValue = "1") int page,
                                                   @RequestParam(value = "size", defaultValue = "10") int size) {
        log.info("LifeBrandedCardController.getBrandedCardList?page={},size={}", page, size);
        return lifeBrandedCardService.getBrandedCardList(page, size);
    }
}
