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 shop.alien.entity.result.R;
import shop.alien.entity.store.dto.StoreActivityDto;
import shop.alien.entity.store.vo.LifeUserVo;
import shop.alien.entity.store.vo.StoreActivityVo;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.store.service.StoreActivityService;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 活动主表 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-05-14
 */
@Slf4j
@Api(tags = {"二期-活动管理"})
@ApiSort(1)
@CrossOrigin
@RestController
@RequestMapping("/store-activity")
@RequiredArgsConstructor
public class StoreActivityController {

    private final StoreActivityService storeActivityService;

    @ApiOperation("web端查询活动")
    @ApiOperationSupport(order = 3)
    @GetMapping("/getActiveList")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "审批状态", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "createdTime", value = "创建时间", dataType = "String", paramType = "query", required = false)
    })
    public R getActiveList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "createdTime", required = false) String createdTime
    ) {
        log.info("StoreActivityController.getActiveList?page={}&size={}&status={}&createdTime={}", page, size, status, createdTime);
        IPage<StoreActivityVo> activeList = storeActivityService.getActiveList(page, size, status, createdTime);
        if (activeList != null) {
            return R.data(activeList);
        }
        return R.fail("失败");
    }

    @ApiOperation("web端查询活动详情")
    @ApiOperationSupport(order = 3)
    @GetMapping("/detail")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "活动id", dataType = "Integer", paramType = "query", required = true),
    })
    public R detail(@RequestParam(value = "id", required = true) Integer id) throws Exception {
        log.info("StoreActivityController.detail?id={}", id);
        StoreActivityVo activityDetail = storeActivityService.getActivityDetail(id);
        if (activityDetail != null) {
            return R.data(activityDetail);
        }
        return R.fail("失败");
    }

    @ApiOperation("web端新增活动")
    @ApiOperationSupport(order = 3)
    @PostMapping("/saveActive")
    public R saveActive(@RequestBody StoreActivityDto storeActivityDto) throws Exception {
        log.info("StoreActivityController.saveActive?storeActivityDto={}", storeActivityDto);
        StoreActivityVo storeActivityVo = storeActivityService.saveActive(storeActivityDto);
        if (storeActivityVo != null) {
            return R.data(storeActivityVo);
        }
        return R.fail("失败");
    }

    @ApiOperation("web端修改活动")
    @ApiOperationSupport(order = 3)
    @PostMapping("/editActive")
    public R editActive(@RequestBody StoreActivityDto storeActivityDto) throws Exception {
        log.info("StoreActivityController.saveActive?storeActivityDto={}", storeActivityDto);
        StoreActivityVo storeActivityVo = storeActivityService.editActive(storeActivityDto);
        if (storeActivityVo != null) {
            return R.data(storeActivityVo);
        }
        return R.fail("失败");
    }

    @ApiOperation("web端审批活动")
    @ApiOperationSupport(order = 3)
    @PostMapping("/approvalActivities")
    public R approvalActivities(@RequestBody StoreActivityDto storeActivityDto) throws Exception {
        log.info("StoreActivityController.saveActive?approvalActivities={}", storeActivityDto);
        StoreActivityVo storeActivityVo = storeActivityService.approvalActivities(storeActivityDto);
        if (storeActivityVo != null) {
            return R.data(storeActivityVo);
        }
        return R.fail("失败");
    }

    @ApiOperation("web端根据关键字获取店铺信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("/getStoreInfoByKeyword")
    public R getStoreInfoByKeyword(@RequestParam("keyword") String keyword) throws Exception {
        log.info("StoreActivityController.getStoreInfoByKeyword?keyword={}", keyword);
        List<StoreInfoVo> storeInfoVos = storeActivityService.getStoreInfoByKeyword(keyword);
        if (storeInfoVos != null) {
            return R.data(storeInfoVos);
        }
        return R.fail("查询失败");
    }

    @ApiOperation("web端根据关键字获取用户端用户信息")
    @ApiOperationSupport(order = 3)
    @GetMapping("/getUserInfoByKeyword")
    public R getUserInfoByKeyword(@RequestParam("keyword") String keyword) throws Exception {
        log.info("StoreActivityController.getUserInfoByKeyword?keyword={}", keyword);
        List<LifeUserVo> lifeUserVos = storeActivityService.getUserInfoByKeyword(keyword);
        if (lifeUserVos != null) {
            return R.data(lifeUserVos);
        }
        return R.fail("查询失败");
    }

    @ApiOperation(value = "web端导出店铺信息")
    @ApiOperationSupport(order = 6)
    @GetMapping("/exportExcel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "审核状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createdTime", value = "创建时间", dataType = "String", paramType = "query"),
    })
    @ResponseBody
    public R exportExcel(@RequestParam(value = "status", required = false) String status
            , @RequestParam(value = "createdTime", required = false) String createdTime) throws IOException {
        log.info("StoreInfoController.exportExcel");
        String excelPath = storeActivityService.exportExcel(status, createdTime);
        return R.data(excelPath);
    }

}
