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


import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.marketing.controller.admin.couponCode.vo.CouponCodePageReqVO;
import com.yc.ship.module.marketing.controller.admin.couponCode.vo.CouponCodeRespVO;
import com.yc.ship.module.marketing.dal.dataobject.coupon.CouponDO;
import com.yc.ship.module.marketing.dal.dataobject.couponCode.CouponCodeDO;
import com.yc.ship.module.marketing.dal.dataobject.couponCodeUseRecord.CouponCodeUseRecordDO;
import com.yc.ship.module.marketing.enums.coupon.CouponEffectiveTypeEnum;
import com.yc.ship.module.marketing.service.CouponCodeUseRecord.CouponCodeUseRecordService;
import com.yc.ship.module.marketing.service.coupon.CouponService;
import com.yc.ship.module.marketing.service.couponCode.CouponCodeService;
import com.yc.ship.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
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.validation.Valid;
import java.util.List;
import java.util.Map;


@Tag(name = "管理后台 - 优惠券码")
@RestController
@RequestMapping("/marketing/couponCode")
@Validated
public class CouponCodeController {

    @Resource
    private CouponCodeService couponCodeService;

    @Resource
    private CouponService couponService;

    @Resource
    private com.yc.ship.module.member.api.user.MemberUserApi MemberUserApi;
    @Resource
    private CouponCodeUseRecordService couponCodeUseRecordService;


    @GetMapping("/page")
    @Operation(summary = "获得优惠券码分页")
    @PreAuthorize("@ss.hasPermission('marketing:couponCode:query')")
    public CommonResult<PageResult<CouponCodeRespVO>> getCouponCodePage(@Valid CouponCodePageReqVO pageReqVO) {
        PageResult<CouponCodeDO> pageResult = couponCodeService.getCouponCodePage(pageReqVO);
        PageResult<CouponCodeRespVO> respResult = BeanUtils.toBean(pageResult, CouponCodeRespVO.class);
        List<CouponCodeRespVO> codeRespVOList = respResult.getList();
        List<CouponCodeDO> codeDoList = pageResult.getList();
        //处理优惠券名称显示
        List<Long> couponIdList = CollectionUtils.convertList(codeDoList, CouponCodeDO::getCouponId);
        List<CouponDO> couponList = couponService.getCouponList(couponIdList);
        Map<Long, CouponDO> longCouponDOMap = CollectionUtils.convertMap(couponList, CouponDO::getId);
        //领用人
        List<Long> memberIdList = CollectionUtils.convertList(codeDoList, CouponCodeDO::getMemberId);
        List<MemberUserRespDTO> userList = MemberUserApi.getUserList(memberIdList);
        Map<Long, MemberUserRespDTO> longMemberUserRespDTOMap = CollectionUtils.convertMap(userList, MemberUserRespDTO::getId);
        //处理使用信息
        List<Long> ids = CollectionUtils.convertList(codeDoList, CouponCodeDO::getId);
        List<CouponCodeUseRecordDO> codeUseRecordDos = couponCodeUseRecordService.getCouponCodeUseRecordListByCodeIds(ids);
        Map<Long, CouponCodeUseRecordDO> longCouponCodeUseRecordDOMap = CollectionUtils.convertMap(codeUseRecordDos, CouponCodeUseRecordDO::getCouponCodeId);

        codeRespVOList.stream().forEach(item  -> {
            String qrcode = QrCodeUtil.generateAsBase64("couponCodeId:"+item.getCode(), new QrConfig(150, 150), ImgUtil.IMAGE_TYPE_PNG);
            item.setQrcode(qrcode);
            MapUtils.findAndThen(longCouponDOMap, item.getCouponId(), couponDO -> {
                item.setCouponName(couponDO.getName());
                if(couponDO.getEffectiveType() == CouponEffectiveTypeEnum.FIXED_TIME.getType()) {
                    item.setEffectiveStart(couponDO.getEffectiveStart());
                    item.setEffectiveEnd(couponDO.getEffectiveEnd());
                }else if (couponDO.getEffectiveType() == CouponEffectiveTypeEnum.RELATIVE_TIME.getType()){
                    item.setEffectiveStart(item.getCreateTime());
                    item.setEffectiveEnd(item.getCreateTime().plusMinutes(couponDO.getEffectiveTime()));
                }
            });
            MapUtils.findAndThen(longMemberUserRespDTOMap, item.getMemberId(), memberUserRespDTO -> {
                item.setMemberName(memberUserRespDTO.getNickname());
            });
            MapUtils.findAndThen(longCouponCodeUseRecordDOMap, item.getId(), couponCodeUseRecordDO -> {
                item.setUseTime(couponCodeUseRecordDO.getCreateTime());
                item.setAmount(couponCodeUseRecordDO.getAmount());
                item.setOrderId(couponCodeUseRecordDO.getOrderId());
            });
        });

        return CommonResult.success(respResult);
    }

    //删除
    @DeleteMapping("/delete")
    @Operation(summary = "删除优惠券码")
    @PreAuthorize("@ss.hasPermission('marketing:couponCode:delete')")
    public CommonResult<Boolean> deleteCouponCode(@RequestParam("id") Long id) {
        couponCodeService.deleteCouponCode(id);
        return CommonResult.success(true);
    }
}