package com.gree.mall.manager.controller.coupon;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.admin.AdminWebsitVO;
import com.gree.mall.manager.bean.coupon.*;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.coupon.CouponLogic;
import com.gree.mall.manager.plus.entity.Coupon;
import com.gree.mall.manager.plus.entity.UserCoupon;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;

@Slf4j
@RestController
@Api(value = "优惠券管理", tags ={"优惠券管理"} )
@RequestMapping(value = "/coupon", produces = "application/json; charset=utf-8")
public class CouponController {

    @Resource
    CouponLogic couponLogic;

    @GetMapping("/list/page")
    @ApiOperation(value = "优惠券列表")
    public ResponseHelper<IPage<CouponPageBean>> page(
            @ApiParam(value = "券名称") @RequestParam(required = false) String couponName,
            @ApiParam(value = "券类型") @RequestParam(required = false) String couponType,
            @ApiParam(value = "券标志") @RequestParam(required = false) String flag,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<CouponPageBean> page = couponLogic.page(couponName, couponType, flag, pageNum, pageSize,request);
        return ResponseHelper.success(page);
    }

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "优惠券列表V2")
    public ResponseHelper<IPage<CouponVO>> list(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CouponVO> couponVOIPage = couponLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
        return ResponseHelper.success(couponVOIPage, new TypeReference<CouponVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "优惠券列表导出V2")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        IPage<CouponVO> couponVOIPage = couponLogic.list(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);        //3.导出
        FieldUtils.exportData(couponVOIPage.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @GetMapping("/detail")
    @ApiOperation(value = "券详情")
    public ResponseHelper<CouponDetailBean> detail(
            @ApiParam(value = "id",required = true) @RequestParam String couponId
    ) throws RemoteServiceException {
        CouponDetailBean detail = couponLogic.detail(couponId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增券")
    public ResponseHelper add(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody CouponBean couponBean)
            throws RemoteServiceException {
        couponLogic.add(request, couponBean);
        return ResponseHelper.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改券")
    public ResponseHelper update(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody CouponBean couponBean)
            throws RemoteServiceException {
        couponLogic.update(request, couponBean);
        return ResponseHelper.success();
    }

    @GetMapping("/cancel")
    @ApiOperation(value = "取消券")
    public ResponseHelper cancel(
            HttpServletRequest request,
            @ApiParam(value = "id",required = true) @RequestParam String couponId)
            throws RemoteServiceException {
        couponLogic.updateFlag(couponId, null, null, "CANCEL");
        return ResponseHelper.success();
    }


    @PostMapping("/reissue")
    @ApiOperation(value = "补发业务员券")
    public ResponseHelper reissueCoupon(
            HttpServletRequest request,
            @ApiParam(value = "object",required = true) @RequestBody ReissueCouponBean reissueCouponBean)
            throws RemoteServiceException {
        couponLogic.reissueCoupon(request, reissueCouponBean);
        return ResponseHelper.success();
    }

    @PostMapping("/change/flag")
    @ApiOperation(value = "券标志")
    public ResponseHelper change(
            @ApiParam(value = "券id",required = true) @RequestParam String couponId,
            @ApiParam(value = "开始时间") @RequestParam(required = false) String startDate,
            @ApiParam(value = "结束时间") @RequestParam(required = false) String endDate,
            @ApiParam(value = "券标志:START=进行中 CANCEL=取消",required = true) @RequestParam String flag
    ) throws RemoteServiceException {
        couponLogic.updateFlag(couponId, startDate, endDate, flag);
        return ResponseHelper.success();
    }

    @GetMapping("/data")
    @ApiOperation(value = "券数据")
    public ResponseHelper<CouponBean> data(
            @ApiParam(value = "id",required = true) @RequestParam String couponId
    ) throws RemoteServiceException {
        CouponDataBean data = couponLogic.data(couponId);
        return ResponseHelper.success(data);
    }

    @GetMapping("/userCouponDetail/query")
    @ApiOperation(value = "用户领取优惠券明细")
    public ResponseHelper<IPage<UserCouponBean>> receiveCouponDetail(
            @ApiParam(value = "我的优惠券id",required = true) @RequestParam String userCouponId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNo,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
            )throws RemoteServiceException{
        return ResponseHelper.success(couponLogic.receiveCouponDetail(userCouponId,pageNo,pageSize));
    }

    @GetMapping("/user/page")
    @ApiOperation(value = "获取领取优惠券用户列表")
    public ResponseHelper<CouponBean> user(
            HttpServletRequest request,
            @ApiParam(value = "手机号",required = false) @RequestParam(required = false) String tel,
            @ApiParam(value = "注册时间",required = false) @RequestParam(required = false) Date registerTime,
            @ApiParam(value = "keyword",required = false) @RequestParam (required = false)String keyword,
            @ApiParam(value = "角色 GENERAL=普通用户 SERVICE=业务员",required = false) @RequestParam (required = false)String type,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNo,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        return ResponseHelper.success(couponLogic.userList(request, tel,registerTime,keyword,type,pageNo,pageSize));
    }


    @GetMapping("/list/spec/select")
    @ApiOperation(value = "优惠券选择活动商品列表")
    public ResponseHelper pageSpecGoods(
            HttpServletRequest request,
            @ApiParam(value = "商品名称") @RequestParam(required = false) String keyword,
            @ApiParam(value = "商品分类id") @RequestParam(required = false) String categoryId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize
    ) throws Exception {
        return ResponseHelper.success(couponLogic.goodsSpecList(request, categoryId,keyword,pageNum, pageSize));
    }


    @GetMapping("/list/export")
    @ApiOperation(value = "优惠券导出")
    public void export(HttpServletRequest request, HttpServletResponse response,
                                                        @ApiParam(value = "券名称") @RequestParam(required = false) String couponName,
                                                        @ApiParam(value = "券类型") @RequestParam(required = false) String couponType,
                                                        @ApiParam(value = "券标志") @RequestParam(required = false) String flag
    ) throws Exception {
        couponLogic.export(request,response,couponName, couponType, flag);
    }


    @PostMapping("/louxx/import")
    @ApiOperation("楼小贤活动券")
    public ResponseHelper louxiaoxian(
            @ApiParam(required = true, value = "excel文件") @RequestParam(required = true) MultipartFile file
    ) throws IOException, ParseException {
        couponLogic.importLxx(file);
        return ResponseHelper.success();
    }


}
