package cn.pluss.platform.controller;

import cn.pluss.platform.activityConsumReturn.ActivityConsumReturnService;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.constants.ProjectConstant;
import cn.pluss.platform.entity.ActivityConsumReturn;
import cn.pluss.platform.util.StringUtil;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/activityConsumReturn")
@Transactional
@Api(value = "ActivityConsumReturn控制类", tags = {"ActivityConsumReturn接口"})
public class ActivityConsumReturnController {

    @Resource
    private ActivityConsumReturnService activityConsumReturnService;

    @GetMapping
    @ApiOperation(value = "获取全部", notes = "返回分页过后的数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0")})
    public Result<PageInfo<ActivityConsumReturn>> list(@RequestParam(defaultValue = "1") Integer page,
                                                       @RequestParam(defaultValue = "10") Integer size) {
        if (size == null || size == 0) {
            size = ProjectConstant.PAGE_SIZE;
        }
        if (page == null || page == 0) {
            page = 1;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", size);
        map.put("offset", (page - 1) * size);
        List<ActivityConsumReturn> activityConsumReturnList = activityConsumReturnService
                .queryActivityConsumReturnPage(map);
        Integer count = activityConsumReturnService.queryActivityConsumReturnPageCount(map);
        // 返回参数
        PageInfo<ActivityConsumReturn> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(),
                activityConsumReturnList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    public Result<Object> add(@RequestBody List<ActivityConsumReturn> activityConsumReturn) {
        if (activityConsumReturn == null) {
            return ResultGenerator.genFailResult("参数错误！");
        }
        if (activityConsumReturn.size() > 0) {
            ActivityConsumReturn activity = new ActivityConsumReturn();
            activity.setMerchantCode(activityConsumReturn.get(0).getMerchantCode());
            activity.setStoreId(activityConsumReturn.get(0).getStoreId());
            List<ActivityConsumReturn> activityList = activityConsumReturnService.queryActivityConsumReturnList(activity);
            for (ActivityConsumReturn a : activityList) {
                activityConsumReturnService.deleteActivityConsumReturn(a);
            }
        }

        for (ActivityConsumReturn activityConsumReturn2 : activityConsumReturn) {
            activityConsumReturn2.setCreateDt(new Date());
            activityConsumReturnService.saveActivityConsumReturn(activityConsumReturn2);
        }
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true)})
    public Result delete(@PathVariable Integer id) {
        ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
        activityConsumReturn.setId(id);
        activityConsumReturnService.deleteActivityConsumReturn(activityConsumReturn);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ActivityConsumReturn", value = "更新的ActivityConsumReturn实例", paramType = "body", dataType = "ActivityConsumReturn", required = true)})
    public Result update(@RequestBody ActivityConsumReturn activityConsumReturn) {
        activityConsumReturnService.updateActivityConsumReturn(activityConsumReturn);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个值", notes = "查看单个项目的内容", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id", paramType = "path", required = true, dataType = "Integer", defaultValue = "0")})
    public Result detail(@PathVariable Long id) {
        ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
        activityConsumReturn.setId(id.intValue());
        activityConsumReturn = activityConsumReturnService.queryActivityConsumReturn(activityConsumReturn);
        return ResultGenerator.genSuccessResult(activityConsumReturn);
    }

}
