package io.controller;



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.dto.ItemDTO;
import io.modules.item.dto.OrderDTO;
import io.modules.item.entity.ItemEntity;
import io.modules.item.entity.OrderEntity;
import io.modules.item.service.ItemService;
import io.modules.item.service.OrderService;
import io.modules.item.service.UserBehaviorService;
import io.modules.item.service.UserService;
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 ItemService itemService;
    @Autowired
    private OrderService orderService;


    @Autowired
    private UserBehaviorService userBehaviorService;
    @Value("${upload.url}")
   private String uploadUrl;
    @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<ItemDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        PageData<ItemDTO> page = itemService.page(params);
        List<ItemDTO> list = page.getList().stream().map(e -> {
            e.setImage(uploadUrl +  e.getImage());
            // 获取评价信息
            List<OrderDTO> orderEntityList =  orderService.getCommintList(e.getId() );
            e.setOrderEntityList(orderEntityList);
            return e;
        }).collect(Collectors.toList());
        page.setList(list);
        return new Result<PageData<ItemDTO>>().ok(page);
    }

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




    //    分析
    @GetMapping("score")
    @Operation(summary = "评分人数排行")
    public Result<List<ItemEntity>> score(){
        List<ItemEntity> list = itemService.score();
        List<ItemEntity> res = list.stream().map(e -> {
            e.setImage(uploadUrl +  e.getImage());
            return e;
        }).collect(Collectors.toList());
        return new Result<List<ItemEntity>>().ok(res);
    }

    //    分析
    @GetMapping("commit")
    @Operation(summary = "评论人数排行")
    public Result<List<ItemEntity>> commit(){
        List<ItemEntity> list = itemService.commit();
        List<ItemEntity> res = list.stream().map(e -> {
            e.setImage(uploadUrl +  e.getImage());
            return e;
        }).collect(Collectors.toList());
        return new Result<List<ItemEntity>>().ok(res);
    }

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

        //获取详情
        ItemDTO data = itemService.get(id);
        data.setImage(data.getImage().replace(uploadUrl,""));
        // 添加预览
        data.setView(data.getView() + 1);

//        更新
        itemService.update(data);
        data.setImage(uploadUrl +  data.getImage());
        //是否收藏
        data.setIsFavorite(userBehaviorService.getIsFavorite(data.getId(),1));
        return new Result<ItemDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result save(@RequestBody ItemDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        itemService.save(dto);
        return new Result();
    }
    @PutMapping
    @Operation(summary = "修改")
    public Result update(@RequestBody ItemDTO dto){        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

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