package com.casic.missiles.controller.equipment;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.equipment.RemindQuantityDTO;
import com.casic.missiles.dto.equipment.StandardEquipmentRequest;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.model.equipment.EquipmentStandardEquipment;
import com.casic.missiles.service.equipment.IEquipmentStandardEquipmentService;
import com.casic.missiles.utils.DictCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 标准装置表 前端控制器
 * </p>
 *
 * @author cuicheng
 * @since 2022-12-15
 */
@RestController
@Api(tags = "标准装置管理接口")
@RequestMapping("/meter/standard")
public class EquipmentStandardEquipmentController extends ExportController {

    @Autowired
    private IEquipmentStandardEquipmentService meterStandardEquipmentService;

    /**
     * 标准装置列表
     */
    @ApiOperation("标准装置（分页）")
    @PostMapping("/listPage")
    @ResponseBody
    public ReturnDTO<PageInfoBT<EquipmentStandardEquipment>> listPage(@RequestBody @Valid StandardEquipmentRequest request) throws Exception {
        Page<EquipmentStandardEquipment> page = PageFactory.defaultPage();
        DictCodeUtils.validDictCode(request);
        return ReturnUtil.success(super.packForBT(meterStandardEquipmentService.listPage(page, request)));
    }

    /**
     * 新建标准装置
     */
    @ApiOperation("新建标准装置")
    @PostMapping("/add")
    @ResponseBody
    public ReturnDTO add(@RequestBody @Valid EquipmentStandardEquipment meterStandardEquipment) throws Exception {
        DictCodeUtils.validDictCode(meterStandardEquipment);
        return meterStandardEquipmentService.add(meterStandardEquipment);
    }
    /**
     * 更新标准装置
     */
    @ApiOperation("更新标准装置")
    @PostMapping("/update")
    @ResponseBody
    public ReturnDTO update(@RequestBody EquipmentStandardEquipment meterStandardEquipment) throws Exception {
        DictCodeUtils.validDictCode(meterStandardEquipment);
        return meterStandardEquipmentService.update(meterStandardEquipment);
    }
    /**
     * 标准装置详情
     */
    @ApiOperation("标准装置详情")
    @PostMapping("/detail")
    @ResponseBody
    public ReturnDTO detail(@RequestBody @Valid IdDTO idDTO) throws Exception {
        return ReturnUtil.success(meterStandardEquipmentService.getById(idDTO.getId()));
    }

    /**
     * 删除标准装置
     */
    @ApiOperation("删除标准装置")
    @PostMapping("/delete")
    @ResponseBody
    public ReturnDTO delete(@RequestBody @Valid IdDTO idDTO) {
        return meterStandardEquipmentService.deleteById(idDTO.getId());
    }

    /**
     * 导出标准装置
     */
    @ApiOperation("导出excel")
    @PostMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(@RequestBody @Valid StandardEquipmentRequest request) throws Exception {
        List<EquipmentStandardEquipment> list;
        DictCodeUtils.validDictCode(request);
        if(!CollectionUtils.isEmpty(request.getIds())){
            StandardEquipmentRequest standardEquipmentRequest = new StandardEquipmentRequest();
            standardEquipmentRequest.setIds(request.getIds());
            list = meterStandardEquipmentService.list(standardEquipmentRequest);
        }else{
            list = meterStandardEquipmentService.list(request);
        }
        if (!CollectionUtils.isEmpty(list)) {
            super.exportExcel(EquipmentStandardEquipment.class, list, ExportEnum.STANDARD_EQUIPMENT.getDescription());
        }
    }

    @ApiOperation("到期提醒列表（分页）")
    @PostMapping("/remindListPage")
    @ResponseBody
    public ReturnDTO<PageInfoBT<EquipmentStandardEquipment>> remindListPage(@RequestBody @Valid StandardEquipmentRequest request) throws Exception {
        Page<EquipmentStandardEquipment> page = PageFactory.defaultPage();
        DictCodeUtils.validDictCode(request);
        return ReturnUtil.success(super.packForBT(meterStandardEquipmentService.remindListPage(page, request)));
    }

    @ApiOperation("到期提醒数量")
    @GetMapping("/remindQuantity")
    @ResponseBody
    public ReturnDTO<RemindQuantityDTO> remindQuantity() {
        return ReturnUtil.success(meterStandardEquipmentService.remindQuantity());
    }

    /**
     * 导出到期提醒列表
     */
    @ApiOperation("导出到期提醒列表")
    @PostMapping("/exportRemindList")
    @ResponseBody
    public void exportRemindList(@RequestBody @Valid StandardEquipmentRequest request, HttpServletResponse response) throws Exception {
        meterStandardEquipmentService.exportRemindList(response, request);
    }

    @ApiOperation("到期提醒-删除")
    @PostMapping("/deleteRemind")
    @ResponseBody
    public ReturnDTO deleteRemind(@RequestBody @Valid IdDTO idDTO) {
        return meterStandardEquipmentService.deleteRemind(idDTO.getId());
    }

    @ApiOperation("到期提醒-提醒")
    @PostMapping("/remind")
    @ResponseBody
    public ReturnDTO remind(@RequestBody @Valid IdDTO idDTO) {
        return meterStandardEquipmentService.remind(idDTO.getId());
    }

}
