package com.izouma.dingdong.web.merchant;

import com.izouma.dingdong.repo.merchant.MerchantRepo;
import com.izouma.dingdong.service.merchant.MerchantService;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.merchant.FullReduction;
import com.izouma.dingdong.service.merchant.FullReductionService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.FullReductionRepo;
import com.izouma.dingdong.utils.ObjUtils;
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.util.List;

@RestController
@RequestMapping("/fullReduction")
@AllArgsConstructor
public class FullReductionController extends BaseController {
    private FullReductionService fullReductionService;
    private FullReductionRepo fullReductionRepo;
    private MerchantRepo merchantRepo;
    private MerchantService merchantService;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public FullReduction save(@RequestBody FullReduction record) {
        if (record.getFullAmount().compareTo(record.getMinusAmount()) <= 0) {
            throw new BusinessException("减的金额不能大于等于满的金额");
        }
        if (record.getId() != null) {
            FullReduction orig = fullReductionRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return fullReductionRepo.save(orig);
        }
        return fullReductionRepo.save(record);
    }


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

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

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

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

    @GetMapping("/my")
    @ApiOperation("商户下的满减")
    public List<FullReduction> my(){
        return fullReductionRepo.findAllByMerchantId(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId()));
        //return fullReductionRepo.findAllByUserId(SecurityUtils.getAuthenticatedUser().getId());
    }
}

