package cn.ysk.cashier.cons.rest;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.domain.ViewHandover;
import cn.ysk.cashier.cons.service.ViewHandoverService;
import cn.ysk.cashier.cons.service.dto.ViewHandoverQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.utils.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.methods.CloseableHttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
* @author admin
* @date 2024-08-05
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "交班管理")
@RequestMapping("/api/tbHandover")
public class ViewHandoverController {

    private final ViewHandoverService viewHandoverService;

    @Value("${cashier.server.wx}")
    private String wxServiceHostname;

    @PostMapping("handoverData")
    @Log("提交交班")
    @ApiOperation("提交交班")
    public ResponseEntity<Object> handoverData(@RequestBody Map<String, Object> map) throws Exception {
        if (CollectionUtils.isEmpty(map) || !map.containsKey("shopId") || !map.containsKey("isprintProduct")) {
            throw new BadRequestException("参数缺失");
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(wxServiceHostname + "/cashierService/common/handoverData");
        String jsonString = JSONUtil.toJSONString(map);
        StringEntity entity = new StringEntity(jsonString);
        post.setEntity(entity);
        post.setHeader("Content-Type", "application/json");

        try (CloseableHttpResponse response = httpClient.execute(post)) {
            if (response.getStatusLine().getStatusCode() == 200) {
                return new ResponseEntity<>(HttpStatus.OK);
            } else {
                return new ResponseEntity<>(HttpStatus.valueOf(response.getStatusLine().getStatusCode()));
            }
        } finally {
            httpClient.close();
        }
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportViewHandover(HttpServletResponse response, ViewHandoverQueryCriteria criteria) throws IOException {
        viewHandoverService.download(viewHandoverService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询交班查询")
    public ResponseEntity<Object> queryViewHandover(ViewHandoverQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(viewHandoverService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增交班查询")
    @ApiOperation("新增交班查询")
    public ResponseEntity<Object> createViewHandover(@Validated @RequestBody ViewHandover resources){
        return new ResponseEntity<>(viewHandoverService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改交班查询")
    @ApiOperation("修改交班查询")
    public ResponseEntity<Object> updateViewHandover(@Validated @RequestBody ViewHandover resources){
        viewHandoverService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除交班查询")
    @ApiOperation("删除交班查询")
    public ResponseEntity<Object> deleteViewHandover(@RequestBody Integer[] ids) {
        viewHandoverService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}