package com.izouma.dingdong.web.merchant;

import com.izouma.dingdong.dto.FilterDTO;
import com.izouma.dingdong.dto.MerchantDTO;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.merchant.MerchantSettings;
import com.izouma.dingdong.service.merchant.MerchantSettingsService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
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.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/settings")
@AllArgsConstructor
public class MerchantSettingsController extends BaseController {
    private MerchantSettingsService merchantSettingsService;
    private MerchantSettingsRepo merchantSettingsRepo;

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


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

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

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

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

    @GetMapping("/search")
    @ApiOperation("搜索栏的商户列表")
    public Page<MerchantDTO> search(PageQuery pageQuery, Double longitude, Double latitude, Long popularTag) {
        Long id = SecurityUtils.getAuthenticatedUser().getId();
        List<MerchantDTO> dtos = merchantSettingsService.showAll(pageQuery, longitude, latitude, popularTag, id);
        return new PageImpl<>(dtos, toPageRequest(pageQuery), dtos.size());
    }

    @GetMapping("/show")
    @ApiOperation("推荐商家下的商户列表")
    public List<MerchantDTO> showAll(Double longitude, Double latitude, Integer sort, FilterDTO filterDto) {
        Long id = SecurityUtils.getAuthenticatedUser().getId();
        return merchantSettingsService.recommended(longitude, latitude, sort, filterDto, id);
    }
}

