package com.yc.ship.module.marketing.controller.admin.couponpackage;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.marketing.controller.admin.couponpackage.vo.CouponPackagePageReqVO;
import com.yc.ship.module.marketing.controller.admin.couponpackage.vo.CouponPackageRespVO;
import com.yc.ship.module.marketing.controller.admin.couponpackage.vo.CouponPackageSaveReqVO;
import com.yc.ship.module.marketing.dal.dataobject.coupon.CouponDO;
import com.yc.ship.module.marketing.dal.dataobject.couponpackage.CouponPackageDO;
import com.yc.ship.module.marketing.service.coupon.CouponService;
import com.yc.ship.module.marketing.service.couponpackage.CouponPackageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 优惠券-券包")
@RestController
@RequestMapping("/marketing/coupon-package")
@Validated
public class CouponPackageController {

    @Resource
    private CouponPackageService couponPackageService;

    @Resource
    private CouponService couponService;

    @PostMapping("/create")
    @Operation(summary = "创建优惠券-券包")
    @PreAuthorize("@ss.hasPermission('marketing:coupon-package:create')")
    public CommonResult<Long> createCouponPackage(@Valid @RequestBody CouponPackageSaveReqVO createReqVO) {
        return success(couponPackageService.createCouponPackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新优惠券-券包")
    @PreAuthorize("@ss.hasPermission('marketing:coupon-package:update')")
    public CommonResult<Boolean> updateCouponPackage(@Valid @RequestBody CouponPackageSaveReqVO updateReqVO) {
        couponPackageService.updateCouponPackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除优惠券-券包")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('marketing:coupon-package:delete')")
    public CommonResult<Boolean> deleteCouponPackage(@RequestParam("id") Long id) {
        couponPackageService.deleteCouponPackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得优惠券-券包")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('marketing:coupon-package:query')")
    public CommonResult<CouponPackageRespVO> getCouponPackage(@RequestParam("id") Long id) {
        CouponPackageDO couponPackage = couponPackageService.getCouponPackage(id);
        return success(BeanUtils.toBean(couponPackage, CouponPackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得优惠券-券包分页")
    @PreAuthorize("@ss.hasPermission('marketing:coupon-package:query')")
    public CommonResult<PageResult<CouponPackageRespVO>> getCouponPackagePage(@Valid CouponPackagePageReqVO pageReqVO) {
        PageResult<CouponPackageDO> pageResult = couponPackageService.getCouponPackagePage(pageReqVO);
        PageResult<CouponPackageRespVO> page = BeanUtils.toBean(pageResult, CouponPackageRespVO.class);
        List<CouponPackageRespVO> list = page.getList();
        List<Long> couponIds = CollectionUtils.convertList(list, CouponPackageRespVO::getCouponId);
        if(!CollectionUtils.isAnyEmpty(couponIds)) {
            List<CouponDO> couponList = couponService.getCouponList(couponIds);
            Map<Long, CouponDO> longCouponDOMap = CollectionUtils.convertMap(couponList, CouponDO::getId);
            list.stream().forEach(item -> {
                item.setCouponName(longCouponDOMap.get(item.getCouponId()).getName());
            });
        }
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出优惠券-券包 Excel")
    @PreAuthorize("@ss.hasPermission('marketing:coupon-package:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportCouponPackageExcel(@Valid CouponPackagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CouponPackageDO> list = couponPackageService.getCouponPackagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "优惠券-券包.xls", "数据", CouponPackageRespVO.class,
                        BeanUtils.toBean(list, CouponPackageRespVO.class));
    }

}