package com.izouma.dingdong.web.merchant;

import com.izouma.dingdong.domain.merchant.MerchantSettings;
import com.izouma.dingdong.dto.MerchantDTO;
import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
import com.izouma.dingdong.service.merchant.MerchantSettingsService;
import com.izouma.dingdong.utils.SecurityUtils;
import com.izouma.dingdong.web.BaseController;
import com.izouma.dingdong.domain.merchant.Merchant;
import com.izouma.dingdong.service.merchant.MerchantService;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.MerchantRepo;
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.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/merchant")
@AllArgsConstructor
public class MerchantController extends BaseController {
    private MerchantService merchantService;
    private MerchantRepo merchantRepo;
    private MerchantSettingsRepo merchantSettingsRepo;
    private MerchantSettingsService merchantSettingsService;

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


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

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

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        //删除商家相关所有信息
        merchantSettingsService.del(id);
    }

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

    @GetMapping("/backAll")
    @ApiOperation("商家所有信息")
    public Page<MerchantDTO> showAll(PageQuery pageQuery) {
        return merchantService.backAll(pageQuery);
    }

    @GetMapping("/my")
    public MerchantDTO my() {
        return merchantService.my(SecurityUtils.getAuthenticatedUser().getId());
    }

    @GetMapping("/getDTO/{id}")
    public MerchantDTO getDTO(@PathVariable Long id) {
        return merchantSettingsService.getDTO(id);
    }

    @PostMapping("/saveDTO")
    @ApiOperation("新建/修改商家")
    public MerchantDTO saveDTO(@RequestBody MerchantDTO record) {
        return merchantService.change(record);
    }

    @GetMapping("/audit")
    @ApiOperation("审核商家")
    public void audit(Long id, Boolean pass, String promo) {
        merchantService.audit(id, pass, promo);
    }

    @GetMapping("/closeMer")
    @ApiOperation("停止营业/开始营业")
    public void closeMer() {
        Long merchantId = merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId());
        merchantService.closeMer(merchantId);
    }


    @GetMapping("/showAll")
    @ApiOperation("商家所有信息")
    public Page<MerchantDTO> showAll(PageQuery pageQuery, Double longitude, Double latitude, String popularTag) {
        List<MerchantDTO> list = merchantService.showAll(pageQuery, longitude, latitude, popularTag, SecurityUtils.getAuthenticatedUser().getId());
        return new PageImpl<>(list, toPageRequest(pageQuery), list.size());

    }

    @GetMapping("/timeTag")
    @ApiOperation("时间标签下的商户")
    public List<MerchantDTO> timeMer(Long timeTagId) {
        return merchantSettingsService.timeMerchant(timeTagId);
    }


    @GetMapping("/index")
    @ApiOperation("用户端首页部分信息")
    public Map<String, Object> index(Double longitude, Double latitude) {
        return merchantSettingsService.index(longitude, latitude);
    }
}

