package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeActivity;
import shop.alien.entity.store.vo.LifeActivityVo;
import shop.alien.store.service.LifeActivityService;
import shop.alien.util.common.FileUpload;

import java.util.Map;

/**
 * 活动
 */
@Api(tags = {"一期-活动"})
@Slf4j
@RestController()
@CrossOrigin
@RequestMapping("/activity")
@RequiredArgsConstructor
public class LifeActivityController {

    private final LifeActivityService lifeActivityService;

    private final FileUpload fileUpload;

    @ApiOperation("活动列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getActivityList")
    public R<IPage<LifeActivityVo>> getActivityList(@RequestParam(defaultValue = "1") int page,
                                                    @RequestParam(defaultValue = "10") int size,
                                                    @RequestParam(value = "phoneId") String phoneId) {
        log.info("LifeActivityController.getActivityList?page={},size={},phoneId={}", page, size, phoneId);
        return R.data(lifeActivityService.getActivityList(page, size, phoneId));
    }

    @ApiOperation("活动详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getActivityById")
    public R<Map<String, Object>> getActivityById(@RequestParam(value = "id", defaultValue = "") String id,
                                                  @RequestParam(value = "currentId", defaultValue = "") String currentId,
                                                  @RequestParam(defaultValue = "1") int page,
                                                  @RequestParam(defaultValue = "10") int size) {
        log.info("LifeActivityController.getActivityById?id={},currentId={},page={},size={}", id, currentId, page, size);
        return R.data(lifeActivityService.getActivityById(id, currentId, page, size));
    }

    @PostMapping("/save")
    public int save(@RequestParam("image1") MultipartFile image1, @RequestParam("pdf") MultipartFile pdf) throws Exception {
        try {
            log.info("LifeActivityController.save?image1={},pdf={}", image1.getOriginalFilename(), pdf.getOriginalFilename());
            LifeActivity lifeActivity = new LifeActivity();
            String path = fileUpload.saveImage(image1);
            lifeActivity.setImagePath(path);
            String pdfPath = fileUpload.saveImage(pdf);
            lifeActivity.setPdfPath(pdfPath);
            return lifeActivityService.save(lifeActivity);
        } catch (Exception e) {
            log.error("LifeActivityController.save ERROR Msg={}", e.getMessage());
            return 0;
        }
    }


    @ApiOperation("活动详情")
    @GetMapping("/getActivityByActivityId")
    public R<Map<String, Object>> getActivityByActivityId(@RequestParam(value = "id", defaultValue = "") String id,
                                                          @RequestParam(value = "currentId", defaultValue = "") String currentId) {
        log.info("LifeActivityController.getActivityById?id={},currentId={}", id, currentId);
        return R.data(lifeActivityService.getActivityByActivityId(id, currentId));
    }

}

