package com.jeeplus.psimanage.foodPurchase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.excel.EasyExcelUtils;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.psimanage.foodPurchase.service.PsiFoodPurchaseService;
import com.jeeplus.psimanage.foodPurchase.service.dto.PsiFoodPurchaseDto;
import com.jeeplus.psimanage.foodPurchase.service.dto.PsiFoodPurchaseExportDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags = "进销存-食材采购")
@RequestMapping("/psi/foodPurchase")
public class PsiFoodPurchaseController {

    @Resource
    private PsiFoodPurchaseService psiFoodPurchaseService;

    @ApiOperation(value = "根据id修改状态")
    @PostMapping("/updateStatusById")
    public void updateStatusById(@RequestBody PsiFoodPurchaseDto dto) {
        psiFoodPurchaseService.updateStatusById(dto);
    }

    @ApiOperation(value = "根据修改单id修改状态")
    @PostMapping("/updateChangeStatusById")
    public void updateChangeStatusById(@RequestBody PsiFoodPurchaseDto dto) {
        psiFoodPurchaseService.updateChangeStatusById(dto);
    }

    @ApiOperation(value = "准备修改单")
    @GetMapping("/prepareChange")
    public ResponseEntity<String> prepareChange(@RequestParam String sourceBasicId) throws Exception {
        String changeId = psiFoodPurchaseService.prepareChange(sourceBasicId);
        return ResponseUtil.newInstance()
                .add("businessTable", "psi_management_food_purchase_change")
                .add("businessId", changeId)
                .ok("操作成功");
    }

    @ApiOperation(value = "删除修改单")
    @GetMapping("/removeChange")
    public ResponseEntity<String> removeChange(@RequestParam String id) {
        return ResponseEntity.ok(psiFoodPurchaseService.removeChange(id));
    }

    @ApiOperation(value = "删除")
    @GetMapping("/remove")
    public ResponseEntity<String> remove(@RequestParam String id) {
        return ResponseEntity.ok(psiFoodPurchaseService.remove(id));
    }

    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody PsiFoodPurchaseDto dto) throws Exception {
        String id = psiFoodPurchaseService.save(dto);
        return ResponseUtil.newInstance()
                .add("businessTable", "psi_management_food_purchase_basic")
                .add("businessId", id)
                .ok("操作成功");
    }

    @ApiOperation(value = "保存修改单")
    @PostMapping("/saveChange")
    public ResponseEntity<String> saveChange(@RequestBody PsiFoodPurchaseDto dto) throws Exception {
        String id = psiFoodPurchaseService.saveChange(dto);
        return ResponseUtil.newInstance()
                .add("businessTable", "psi_management_food_purchase_change")
                .add("businessId", id)
                .ok("操作成功");
    }

    @ApiOperation(value = "审批通过后应用修改")
    @PostMapping("/approveChange")
    public ResponseEntity<String> approveChange(@RequestBody PsiFoodPurchaseDto dto) throws Exception {
        String id = psiFoodPurchaseService.approveChange(dto);
        return ResponseUtil.newInstance()
                .add("businessTable", "psi_management_food_purchase_change")
                .add("businessId", id)
                .ok("操作成功");
    }

    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<PsiFoodPurchaseDto> findById(@RequestParam String id) {
        return ResponseEntity.ok(psiFoodPurchaseService.findById(id));
    }

    @ApiOperation(value = "根据修改单id查询")
    @GetMapping("/findChangeById")
    public ResponseEntity<PsiFoodPurchaseDto> findChangeById(@RequestParam String id) {
        return ResponseEntity.ok(psiFoodPurchaseService.findChangeById(id));
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<PsiFoodPurchaseDto>> list(Page<PsiFoodPurchaseDto> page, PsiFoodPurchaseDto dto) throws Exception {
        return ResponseEntity.ok(psiFoodPurchaseService.list(page, dto));
    }

    @ApiLog(value = "导出食材采购数据", type = LogTypeEnum.EXPORT)
    @ApiOperation(value = "导出食材采购数据")
    @GetMapping("/exportFile")
    public void exportFile(Page<PsiFoodPurchaseDto> page, PsiFoodPurchaseDto dto, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<PsiFoodPurchaseExportDto> result;
        if (ExportMode.current.equals(options.getMode())) {
            result = psiFoodPurchaseService.exportList(page, dto);
        } else if (ExportMode.selected.equals(options.getMode())) {
            result = psiFoodPurchaseService.exportList(page, dto).stream().filter(item ->
                    options.getSelectIds().contains(item.getId())
            ).collect(Collectors.toList());
        } else {
            page.setSize(-1);
            page.setCurrent(0);
            result = psiFoodPurchaseService.exportList(page, dto);
        }
        EasyExcelUtils.newInstance().exportExcel(result, sheetName, PsiFoodPurchaseExportDto.class, fileName, null, response);
    }

    @ApiOperation(value = "采购金额统计")
    @GetMapping("/statistics")
    public ResponseEntity<Map<String, BigDecimal>> statistics(PsiFoodPurchaseDto dto) {
        Map<String, BigDecimal> result = new HashMap<>(1);
        result.put("totalPrice", psiFoodPurchaseService.statistics(dto));
        return ResponseEntity.ok(result);
    }
}
