package cn.pluss.platform.controller;

import cn.pluss.platform.activityActivate.ActivityActivateService;
import cn.pluss.platform.activityConsumReturn.ActivityConsumReturnService;
import cn.pluss.platform.activityRecharge.ActivityRechargeService;
import cn.pluss.platform.activityRecommend.ActivityRecommendService;
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.*;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.util.EmojiUtil;
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.web.bind.annotation.*;

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

/**
 * @author DJH
 */
@RestController
@Slf4j
@RequestMapping("/activityActivate")
@Api(value = "ActivityActivate控制类", tags = {"ActivityActivate接口"})
public class ActivityActivateController {
    @Resource
    private ActivityActivateService activityActivateService;

    @Resource
    private ActivityConsumReturnService activityConsumReturnService;

    @Resource
    private ActivityRechargeService activityRechargeService;

    @Resource
    private ActivityRecommendService activityRecommendService;

    @Resource
    private MerchantStoreService merchantStoreService;

    @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<ActivityActivate>> 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<ActivityActivate> activityActivateList = activityActivateService.queryActivityActivatePage(map);

        for (ActivityActivate activityActivate : activityActivateList) {
            activityActivate.setName(EmojiUtil.emojiRecovery2(activityActivate.getName()));
        }

        Integer count = activityActivateService.queryActivityActivatePageCount(map);
        // 返回参数
        PageInfo<ActivityActivate> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(),
                activityActivateList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    public Result<Object> add(@RequestBody ActivityActivate activityActivate) {

        ActivityActivate activity = new ActivityActivate();
        activity.setMerchantCode(activityActivate.getMerchantCode());
        activity.setStoreId(activityActivate.getStoreId());
        List<ActivityActivate> activityList = activityActivateService.queryActivityActivateList(activity);
        for (ActivityActivate a : activityList) {
            activityActivateService.deleteActivityActivate(a);
        }
        if(StringUtil.isEmpty(activityActivate.getStoreId())){
            MerchantStore store = merchantStoreService.queryMerchantStore(new MerchantStore().setMerchantCode(activityActivate.getMerchantCode()));
            if(store == null){
                return ResultGenerator.genFailResult("门店信息有误！",null);
            }
            activityActivate.setStoreId(store.getStoreId());
            activityActivate.setStoreName(store.getStoreName());
        }
        activityActivate.setCreateDt(new Date());
        activityActivateService.saveActivityActivate(activityActivate);
        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<Object> delete(@PathVariable Integer id) {
        ActivityActivate activityActivate = new ActivityActivate();
        activityActivate.setId(id);
        activityActivateService.deleteActivityActivate(activityActivate);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ActivityActivate", value = "更新的ActivityActivate实例", paramType = "body", dataType = "ActivityActivate", required = true)})
    public Result<Object> update(@RequestBody ActivityActivate activityActivate) {
        activityActivateService.updateActivityActivate(activityActivate);
        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<Object> detail(@PathVariable Long id) {
        ActivityActivate activityActivate = new ActivityActivate();
        activityActivate.setId(id.intValue());
        activityActivate = activityActivateService.queryActivityActivate(activityActivate);
        return ResultGenerator.genSuccessResult(activityActivate);
    }

    @GetMapping("/queryAcitvity")
    @ApiOperation(tags = {"页面-设置会员卡"}, value = "获取店铺活动", notes = "获取店铺活动", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantCode", value = "商户Id", paramType = "merchantCode", required = true, dataType = "merchantCode"),
            @ApiImplicitParam(name = "storeId", value = "门店Id", paramType = "storeId", required = true, dataType = "storeId"),
    })
    public Result<Object> queryActivity(@RequestParam String merchantCode, String storeId) {
        Map<String, Object> result = new HashMap<>(16);
        if (StringUtil.isEmpty(merchantCode)) {
            return ResultGenerator.genFailResult("商家编码不能为空");
        }

        ActivityActivate activityActivate = new ActivityActivate();
        activityActivate.setMerchantCode(merchantCode);
        activityActivate.setStoreId(storeId);
        List<ActivityActivate> queryActivityActivateList = activityActivateService.queryActivityActivateList(activityActivate);
        ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
        activityConsumReturn.setMerchantCode(merchantCode);
        activityConsumReturn.setStoreId(storeId);
        List<ActivityConsumReturn> queryActivityConsumReturnList = activityConsumReturnService.queryActivityConsumReturnList(activityConsumReturn);
        ActivityRecharge activityRecharge = new ActivityRecharge();
        activityRecharge.setMerchantCode(merchantCode);
        activityRecharge.setStoreId(storeId);
        List<ActivityRecharge> queryActivityRechargeList = activityRechargeService.queryActivityRechargeList(activityRecharge);
        ActivityRecommend activityRecommend = new ActivityRecommend();
        activityRecommend.setMerchantCode(merchantCode);
        activityRecommend.setStoreId(storeId);
        List<ActivityRecommend> queryActivityRecommendList = activityRecommendService.queryActivityRecommendList(activityRecommend);
        result.put("activityActivateList", queryActivityActivateList);
        result.put("activityConsumReturnList", queryActivityConsumReturnList);
        result.put("activityRechargeList", queryActivityRechargeList);
        result.put("activityRecommendList", queryActivityRecommendList);

        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("deleteAcitvityByType")
    @ApiOperation(value = "根据type删除对应的活动", notes = "根据type删除对应的活动 0：充值 1消费返 2激活送 ", httpMethod = "POST")
    public Result<Object> deleteActivityByType(@RequestBody ActivityActivate activityActivate) {
        String type = activityActivate.getType();
        Integer id = activityActivate.getId();

		if ("0".equals(type)) {
			ActivityRecharge activityRecharge = new ActivityRecharge();
			activityRecharge.setId(id);
			activityRechargeService.deleteActivityRecharge(activityRecharge);
		} else if ("1".equals(type)) {
			ActivityConsumReturn activityConsumReturn = new ActivityConsumReturn();
			activityConsumReturn.setId(id);
			activityConsumReturnService.deleteActivityConsumReturn(activityConsumReturn);
		} else if ("2".equals(type)) {
			activityActivateService.deleteActivityActivate(activityActivate);
		} else {
			return ResultGenerator.genSuccessResult("删除失败");
		}
		return ResultGenerator.genSuccessResult("删除成功");
    }

    @PostMapping("delActByType")
    public Result<Object> delActByType(@RequestBody ActivityActivate activityActivate) {
        activityActivateService.delActByType(activityActivate);
        return ResultGenerator.genSuccessResult("删除成功", null);
    }
}
