package com.ssm.controller;


import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.dto.CouponShareDto;
import com.ssm.manage.entity.CouponShare;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.CouponShareRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.CouponShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * (CouponShare)表控制层
 *
 * @author makejava
 * @since 2022-06-11 15:00:06
 */
@RestController
@RequestMapping("couponShare")
@Api(tags = "优惠券分享")
@Slf4j
public class CouponShareController {
    /**
     * 服务对象
     */
    @Autowired
    private CouponShareService couponShareService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
//@RequiresPermissions("system:couponShare:query")
    public CouponShare get(@PathVariable Serializable id) {
        return this.couponShareService.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
//    @RequiresPermissions("system:couponShare:query")
    public PageResponse listPage(CouponShareDto dto) {
        PageResponse<CouponShare> pageResponse = new PageResponse<>();
        PageInfo<CouponShare> pageInfo = new PageInfo<>();
        try {
            pageInfo = couponShareService.queryByPage(dto);
        } catch (Exception e) {
            log.error("SsmProjectController.page", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    /**
     * 查询自己拥有的优惠券和自己分润金额
     */
    @PostMapping("personalPage")
    @ApiOperation("个人优惠券页面")
    public PageResponse personalPage(@RequestBody CouponShareDto dto){
        PageResponse<CouponShareRequest> pageResponse = new PageResponse<>();
        List<CouponShareRequest> couponShares = new ArrayList<>();
        try {
            couponShares = couponShareService.personalPage(dto);
        } catch (Exception e) {
            log.error("SsmProjectController.personalPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setData(couponShares);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    /**
     * 新增
     *
     * @param couponShare
     * @return
     */
    @PostMapping
    @ApiOperation("新增")
//    @RequiresPermissions("system:couponShare:add")
    public GenericResponse add(@RequestBody CouponShareRequest couponShare) throws SmsException {

        GenericResponse<CouponShare> genericResponse = new GenericResponse<>();
//        String key = SessionHelp.getLoginAccount(session);
//        Long storeId = SessionHelp.getStoreId(session);
        int i = 0;
        CouponShare couponShare1 = new CouponShare();
        BeanUtils.copyProperties(couponShare,couponShare1);
        try {
            i = couponShareService.add(couponShare1);
        } catch (Exception e) {
            log.error("CouponShareController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }else if(i==0){
//            不能重复领取优惠卷
            genericResponse.setCodeAndMsg(ResponseCode.HEAVY_LOAD);
            genericResponse.setData(couponShare1);
        }
        return genericResponse;
    }


//    @PostMapping("/share")
//    @ApiOperation("分享获取两个用户数据")
//    public int share(@RequestBody CouponShareDto dto) {
//        return this.couponShareService.share(dto);
//    }



    /**
     * 编辑
     *
     * @param couponShare
     * @return
     */
    @PutMapping
    @ApiOperation("编辑")
//    @RequiresPermissions("system:couponShare:edit")
    public int edit(@RequestBody CouponShare couponShare) {
        return this.couponShareService.edit(couponShare);
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除")
//    @RequiresPermissions("system:couponShare:delete")
    public int delete(@PathVariable Serializable id) {
        return this.couponShareService.delete(id);
    }

}