package com.greenorange.promotion.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.annotation.SysLog;
import com.greenorange.promotion.common.BaseResponse;
import com.greenorange.promotion.common.ErrorCode;
import com.greenorange.promotion.common.ResultUtils;
import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.exception.ThrowUtils;
import com.greenorange.promotion.model.dto.CommonBatchRequest;
import com.greenorange.promotion.model.dto.promoCode.PromoCodeAddRequest;
import com.greenorange.promotion.model.dto.promoCode.PromoCodeQueryRequest;
import com.greenorange.promotion.model.dto.promoCode.PromoCodeUpdateRequest;
import com.greenorange.promotion.model.entity.Project;
import com.greenorange.promotion.model.entity.PromoCode;
import com.greenorange.promotion.model.entity.UserInfo;
import com.greenorange.promotion.model.vo.promoCode.PromoCodeVO;
import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.project.ProjectService;
import com.greenorange.promotion.service.project.PromoCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.greenorange.promotion.model.dto.CommonRequest;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 推广码 控制器
 */
@RestController
@RequestMapping("promoCode")
@Slf4j
@Tag(name = "推广码管理")
@Transactional
public class PromoCodeController {

    @Resource
    private PromoCodeService promoCodeService;

    @Resource
    private CommonService commonService;

    @Resource
    private ProjectService projectService;

    /**
     * web端管理员添加推广码
     * @param promoCodeAddRequest 推广码添加请求体
     * @return 是否添加成功
     */
    @PostMapping("add")
    @Operation(summary = "web端管理员添加推广码", description = "参数：推广码添加请求体，权限：管理员，方法名：addPromoCode")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "推广码管理", content = "web端管理员添加推广码")
    public BaseResponse<Boolean> addPromoCode(@Valid @RequestBody PromoCodeAddRequest promoCodeAddRequest) {
        Long proId = promoCodeAddRequest.getProjectId();
        Project pro = projectService.getById(proId);
        ThrowUtils.throwIf(pro == null, ErrorCode.OPERATION_ERROR, "项目不存在");
        PromoCode promoCode = commonService.copyProperties(promoCodeAddRequest, PromoCode.class);
        promoCodeService.save(promoCode);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id修改推广码信息
     * @param promoCodeUpdateRequest 推广码更新请求体
     * @return 是否更新成功
     */
    @PostMapping("update")
    @Operation(summary = "web端管理员更新推广码", description = "参数：推广码更新请求体，权限：管理员，方法名：updatePromoCode")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "推广码管理", content = "web端管理员根据id修改推广码信息")
    public BaseResponse<Boolean> updatePromoCode(@Valid @RequestBody PromoCodeUpdateRequest promoCodeUpdateRequest) {
        PromoCode promoCode = commonService.copyProperties(promoCodeUpdateRequest, PromoCode.class);
        promoCodeService.updateById(promoCode);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id删除推广码
     * @param commonRequest 推广码删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delete")
    @Operation(summary = "web端管理员根据id删除推广码", description = "参数：推广码删除请求体，权限：管理员，方法名：delPromoCode")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "推广码管理", content = "web端管理员根据id删除推广码")
    public BaseResponse<Boolean> delPromoCode(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        PromoCode promoCode = promoCodeService.getById(id);
        Boolean promoCodeStatus = promoCode.getPromoCodeStatus();
        ThrowUtils.throwIf(promoCodeStatus, ErrorCode.OPERATION_ERROR, "当前推广码正在使用中，无法删除");
        promoCodeService.removeById(id);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员批量删除推广码
     * @param commonBatchRequest 推广码批量删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delBatch")
    @Operation(summary = "web端管理员批量删除推广码", description = "参数：推广码批量删除请求体，权限：管理员，方法名：delBatchPromoCode")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "推广码管理", content = "web端管理员批量删除推广码")
    public BaseResponse<Boolean> delBatchPromoCode(@Valid @RequestBody CommonBatchRequest commonBatchRequest) {
        promoCodeService.delBatchPromoCode(commonBatchRequest);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id查询推广码
     * @param commonRequest 推广码查询请求体
     * @return 推广码信息
     */
    @PostMapping("queryById")
    @Operation(summary = "web端管理员根据id查询推广码", description = "参数：推广码查询请求体，权限：管理员，方法名：queryPromoCodeById")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "推广码管理", content = "web端管理员根据id查询推广码")
    public BaseResponse<PromoCodeVO> queryPromoCodeById(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        PromoCode promoCode = promoCodeService.getById(id);
        PromoCodeVO promoCodeVO = commonService.copyProperties(promoCode, PromoCodeVO.class);
        return ResultUtils.success(promoCodeVO);
    }



    /**
     * web端管理员根据项目id查询推广码
     * @param commonRequest 项目id
     * @return 推广码信息
     */
    @PostMapping("queryByPid")
    @Operation(summary = "web端管理员根据id查询推广码", description = "参数：项目id，权限：管理员，方法名：queryPromoCodeByPid")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "推广码管理", content = "web端管理员根据项目id查询推广码")
    public BaseResponse<List<PromoCodeVO>> queryPromoCodeByPid(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        LambdaQueryWrapper<PromoCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PromoCode::getProjectId, id);
        List<PromoCode> promoCodeList = promoCodeService.list(lambdaQueryWrapper);
        List<PromoCodeVO> promoCodeVOS = commonService.convertList(promoCodeList, PromoCodeVO.class);
        return ResultUtils.success(promoCodeVOS);
    }


}