package io.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.annotation.Login;
import io.common.constant.Constant;
import io.common.page.PageData;
import io.common.utils.Result;
import io.common.validator.AssertUtils;
import io.common.validator.ValidatorUtils;
import io.common.validator.group.AddGroup;
import io.common.validator.group.DefaultGroup;
import io.common.validator.group.UpdateGroup;
import io.modules.item.dao.ItemDao;
import io.modules.item.dto.HouseDTO;
import io.modules.item.dto.OrderDTO;
import io.modules.item.entity.HouseEntity;
import io.modules.item.service.HouseService;
import io.modules.item.service.OrderService;
import io.modules.item.service.UserBehaviorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@CrossOrigin
@RestController
@RequestMapping("/api/item")
@Tag(name = "主表")
public class ItemController {
    @Autowired
    private HouseService houseService;

    @Autowired
    private ItemDao itemDao;

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserBehaviorService userBehaviorService;




    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "status", description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = "view", description = "预览排序", in = ParameterIn.QUERY, ref = "String"),
    })
    public Result<PageData<HouseDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {

        PageData<HouseDTO> page = houseService.page(params);
        List<HouseDTO> list = page.getList().stream().map(e -> {

            // 获取评价信息
            List<OrderDTO> orderEntityList = orderService.getCommintList(e.getId());
            e.setOrderEntityList(orderEntityList);
            return e;
        }).collect(Collectors.toList());
        page.setList(list);
        return new Result<PageData<HouseDTO>>().ok(page);
    }


    @Login
    @GetMapping("push")
    @Operation(summary = "分页")
    public Result<List<HouseDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params,@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        params.put("userId",userId.toString());
        List<HouseDTO> page = houseService.list(params);
        List<HouseDTO> list = page.stream().map(e -> {
            // 获取评价信息
            List<OrderDTO> orderEntityList = orderService.getCommintList(e.getId());
            e.setOrderEntityList(orderEntityList);
            return e;
        }).collect(Collectors.toList());
        return new Result<List<HouseDTO>>().ok(list);
    }




    @Login
    @GetMapping("list")
    @Operation(summary = "查询收藏")
    public Result<List<HouseEntity>> list(@Parameter(hidden = true) @RequestAttribute("userId") Long userId) {
        List<HouseEntity> page = houseService.listUser(userId);
        List<HouseEntity> list = page.stream().map(e -> {
            return e;
        }).collect(Collectors.toList());
        return new Result<List<HouseEntity>>().ok(list);
    }

    @GetMapping("hex")
    public Result<HouseEntity> hex(@RequestParam String hexId) {
        LambdaQueryWrapper<HouseEntity> lwq = new LambdaQueryWrapper<>();
        lwq.eq(HouseEntity::getHex, hexId);
        List<HouseEntity> itemEntities = itemDao.selectList(lwq);
        if (itemEntities.isEmpty()) {
            return new Result<HouseEntity>().error("查询不到相关商品");
        }
        HouseEntity itemEntity = itemEntities.get(0);

        return new Result<HouseEntity>().ok(itemEntity);
    }

    //    分析
    @GetMapping("score")
    @Operation(summary = "评分人数排行")
    public Result<List<HouseEntity>> score() {
        List<HouseEntity> list = houseService.score();
        List<HouseEntity> res = list.stream().map(e -> {

            return e;
        }).collect(Collectors.toList());
        return new Result<List<HouseEntity>>().ok(res);
    }

    //    分析
    @GetMapping("commit")
    @Operation(summary = "评论人数排行")
    public Result<List<HouseEntity>> commit() {
        List<HouseEntity> list = houseService.commit();
        List<HouseEntity> res = list.stream().map(e -> {

            return e;
        }).collect(Collectors.toList());
        return new Result<List<HouseEntity>>().ok(res);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<HouseDTO> get(@PathVariable("id") Long id) {

        //获取详情
        HouseDTO data = houseService.get(id);
        // 添加预览
        data.setView(data.getView() + 1);
//        更新
        houseService.update(data);

        //是否收藏
        data.setIsFavorite(userBehaviorService.getIsFavorite(data.getId(), 1));
        return new Result<HouseDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody HouseDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        houseService.save(dto);
        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result update(@RequestBody HouseDTO dto) {        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);


        houseService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        houseService.delete(ids);
        return new Result();
    }
}
