package cn.pluss.platform.controller;

import cn.pluss.platform.activityRecharge.ActivityRechargeService;
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.ActivityRecharge;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DJH
 */
@RestController
@RequestMapping("/activityRecharge")
@RequiredArgsConstructor
@Slf4j
@Transactional
@Api(value = "ActivityRecharge控制类", tags = {"ActivityRecharge接口"})
public class ActivityRechargeController {


    private final ActivityRechargeService activityRechargeService;

    private final MerchantStoreService merchantStoreService;

    private final MerchantBaseInfoService merchantBaseInfoService;

    @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<ActivityRecharge>> 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<ActivityRecharge> activityRechargeList = activityRechargeService.queryActivityRechargePage(map);
        Integer count = activityRechargeService.queryActivityRechargePageCount(map);
        // 返回参数
        PageInfo<ActivityRecharge> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(),
                activityRechargeList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    public Result<List<ActivityRecharge>> add(@RequestBody List<ActivityRecharge> activityRechargeList) {
        MerchantStore merchantStore;
        MerchantBaseInfo merchantBaseInfo;

        if (activityRechargeList != null && !activityRechargeList.isEmpty()) {
            MerchantStore queryMerchantStore = new MerchantStore();
            queryMerchantStore.setMerchantCode(activityRechargeList.get(0).getMerchantCode());
            merchantStore = merchantStoreService.queryMerchantStore(queryMerchantStore);

            MerchantBaseInfo queryMerchantBaseInfo = new MerchantBaseInfo();
            queryMerchantBaseInfo.setMerchantCode(activityRechargeList.get(0).getMerchantCode());
            merchantBaseInfo = merchantBaseInfoService.queryMerchantBaseInfo(queryMerchantBaseInfo);

            ActivityRecharge activity = new ActivityRecharge();
            activity.setMerchantCode(merchantStore.getMerchantCode());
            List<ActivityRecharge> activityList = activityRechargeService.queryActivityRechargeList(activity);
            for (ActivityRecharge a : activityList) {
                activityRechargeService.deleteActivityRecharge(a);
            }

            for (ActivityRecharge activityRecharge2 : activityRechargeList) {
                activityRecharge2.setCreateDt(new Date());
                activityRecharge2.setMerchantName(merchantBaseInfo.getAlias());
                activityRecharge2.setStoreId(merchantStore.getStoreId());
                activityRecharge2.setStoreName(merchantStore.getStoreName());
                activityRechargeService.saveActivityRecharge(activityRecharge2);
            }
        }
        return ResultGenerator.genSuccessResult(activityRechargeList);
    }

    @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) {
        ActivityRecharge activityRecharge = new ActivityRecharge();
        activityRecharge.setId(id);
        activityRechargeService.deleteActivityRecharge(activityRecharge);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ActivityRecharge", value = "更新的ActivityRecharge实例", paramType = "body", dataType = "ActivityRecharge", required = true)})
    public Result update(@RequestBody ActivityRecharge activityRecharge) {
        activityRechargeService.updateActivityRecharge(activityRecharge);
        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) {
        ActivityRecharge activityRecharge = new ActivityRecharge();
        activityRecharge.setId(id.intValue());
        activityRecharge = activityRechargeService.queryActivityRecharge(activityRecharge);
        return ResultGenerator.genSuccessResult(activityRecharge);
    }

}
