package com.izouma.dingdong.web;

import com.izouma.dingdong.domain.Appraisal;
import com.izouma.dingdong.dto.AppraisalMerDTO;
import com.izouma.dingdong.enums.AppraisalSort;
import com.izouma.dingdong.service.AppraisalService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.AppraisalRepo;
import com.izouma.dingdong.service.merchant.MerchantService;
import com.izouma.dingdong.utils.ObjUtils;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Security;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/appraisal")
@AllArgsConstructor
public class AppraisalController extends BaseController {
    private AppraisalService appraisalService;
    private AppraisalRepo appraisalRepo;
    private MerchantService merchantService;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public Appraisal save(@RequestBody Appraisal record) {
        if (record.getId() != null) {
            Appraisal orig = appraisalRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return appraisalRepo.save(orig);
        }
        return appraisalRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/all")
    public Page<Appraisal> all(PageQuery pageQuery) {
        return appraisalRepo.findAll(toSpecification(pageQuery, Appraisal.class), toPageRequest(pageQuery));
    }

    @GetMapping("/get/{id}")
    public Appraisal get(@PathVariable Long id) {
        return appraisalRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        appraisalRepo.deleteById(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<Appraisal> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @ApiOperation("用户评价")
    @PostMapping("/userAppraisal")
    public Appraisal userAppraisal(Appraisal appraisal) {
        //商家id和骑手id可不传
        return appraisalService.userAppraisal(appraisal);
    }

    @ApiOperation("商家回复评价")
    @GetMapping("/merchantReply")
    public Appraisal merchantApply(@RequestParam Long id, String reply) {
        Appraisal appraisal = appraisalRepo.findById(id).orElseThrow(new BusinessException("评价不存在"));
        appraisal.setMerchantReply(reply);
        return appraisalRepo.save(appraisal);
    }

    /**
     * 显示所有评价
     * @param appraisalSort  排序规则
     * @return 所有
     */
    @GetMapping("/my")
    @ApiOperation("显示商户的所有评价")
    public List<AppraisalMerDTO> my(@RequestParam Long merchantId,@RequestParam AppraisalSort appraisalSort) {
        //List<Appraisal> allByMerchantId = appraisalRepo.findAllByMerchantId(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId()));
        List<Appraisal> appraisals = appraisalService.appraisalsSort(merchantId, appraisalSort);
        return appraisals.stream().map(appraisalService::toDTO).collect(Collectors.toList());

    }

    @GetMapping("/order")
    @ApiOperation("订单评价")
    public Appraisal order(Long orderId) {
        return appraisalRepo.findByOrderInfoId(orderId).orElseThrow(new BusinessException("无评价"));
    }

    @GetMapping("/goods")
    @ApiOperation("商品评价")
    public List<AppraisalMerDTO> goods(Long goodsId) {
        return appraisalService.goodsAppraisals(goodsId);
    }

}

