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.AutomaticResponse;
import com.izouma.dingdong.service.merchant.AutomaticResponseService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.AutomaticResponseRepo;
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("/automaticResponse")
@AllArgsConstructor
public class AutomaticResponseController extends BaseController {
    private AutomaticResponseService automaticResponseService;
    private AutomaticResponseRepo automaticResponseRepo;
    private MerchantService merchantService;
    private MerchantRepo merchantRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public AutomaticResponse save(@RequestBody AutomaticResponse record) {
        if (record.getUserId() == null) {
            //如果商家ID为空
            record.setUserId((SecurityUtils.getAuthenticatedUser().getId()));
        }
        merchantRepo.findByUserId(record.getUserId()).orElseThrow(new BusinessException("商家不存在"));
        if (record.getId() != null) {
            AutomaticResponse orig = automaticResponseRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return automaticResponseRepo.save(orig);
        }
        return automaticResponseRepo.save(record);
    }


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

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

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

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

    @ApiOperation("商家下的所有自动回复")
    @GetMapping("/my")
    public AutomaticResponse my() {
        return automaticResponseRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId());
    }
}

