package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeBrowseRecord;
import shop.alien.store.service.LifeBrowseRecordService;
import shop.alien.util.common.ListToPage;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 浏览记录
 */
@Api(tags = {"2.5期-浏览记录"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/browseRecord")
@RequiredArgsConstructor
public class LifeBrowseRecordController {

    private final LifeBrowseRecordService lifeBrowseRecordService;

    @ApiOperation("添加浏览记录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "门店id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "dynamicsId", value = "动态id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "couponId", value = "团购id", dataType = "String", paramType = "query")
    })
    @PostMapping("/save")
    public R<Boolean> save(@RequestParam(value = "userId",required = false) String userId, @RequestParam(value = "storeId",required = false) String storeId, @RequestParam(value = "dynamicsId",required = false) String dynamicsId,@RequestParam(value = "couponId",required = false) String couponId) {
        log.info("LifeBrowseRecordController.save?userId={},storeId={},dynamicsId={},couponId={}", userId,storeId, dynamicsId, couponId);
        LifeBrowseRecord record = new LifeBrowseRecord();
        record.setUserId(userId);
        record.setStoreId(storeId);
        record.setDynamicsId(dynamicsId);
        record.setCouponId(couponId);
        if (lifeBrowseRecordService.save(record) > 0) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    @ApiOperation("查询浏览记录")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "类型 1：商家；2：团购；3：动态", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query")})
    @GetMapping("/getBrowseRecord")
    public R<IPage<Map<String, Object>>> getBrowseRecord(@RequestParam("userId") String userId,@RequestParam("type") String type, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        log.info("LifeBrowseRecordController.getBrowseRecord?userId={},type={},page={},size={}", userId,type, page, size);
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, List<Map<String, Object>>> map = lifeBrowseRecordService.getBrowseRecord(userId,type);
        for (Map.Entry<String, List<Map<String, Object>>> entry : map.entrySet()) {
            String year = entry.getKey();
            List<Map<String, Object>> records = entry.getValue();
            Map<String, Object> newMap = new HashMap<>();
            newMap.put("year", year);
            newMap.put("records", records);
            dataList.add(newMap);
        }
        // 将排序后的结果重新赋值给 dataList
        dataList = dataList.stream()
                .sorted(Comparator.comparing(a -> a.get("year").toString(), Comparator.reverseOrder()))
                .collect(Collectors.toList());
        return R.data(ListToPage.setPage(dataList, page, size));
    }

    @ApiOperation("清除浏览记录")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "recordId", value = "浏览记录id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型：1店铺，2团购，3动态", dataType = "String", paramType = "query")})
    @PostMapping("/clearBrowseRecord")
    public R<Boolean> clearBrowseRecord(@RequestParam("userId") String userId, @RequestParam(value = "recordId",required = false) String recordId,@RequestParam(value = "type",required = false) String type) {
        log.info("LifeBrowseRecordController.clearBrowseRecord?userId={}&recordId={}&type={}", userId, recordId,type);
        int num = lifeBrowseRecordService.clearBrowseRecord(userId, recordId,type);
        if (num == 0) {
            return R.fail("操作失败");
        }
        return R.success("操作成功");
    }

}
