package com.ozs.web.controller.accountmanagment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.file.FileUtils;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.service.entity.BaseAccountManagement;
import com.ozs.service.entity.BaseLineManagement;
import com.ozs.service.entity.vo.BaseAccountManagementVo;
import com.ozs.service.entity.vo.BaseLineManagementVo;
import com.ozs.service.service.BaseLineManagementService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 线路台账管理表前端控制器
 * </p>
 *
 * @author ozs
 * @since 2023-02-17
 */
@RestController
@RequestMapping("/service/baseLineManagement")
@Slf4j
public class BaseLineManagementController extends BaseController {
    @Resource
    BaseLineManagementService baseLineManagementService;

    /**
     * 线路台账管理分页
     *
     * @param baseLineManagementVo
     * @return
     */
    @ApiOperation(value = "线路台账管理分页")
    @PostMapping("/list")
    public AjaxResult selectHomeNotice(@RequestBody BaseLineManagementVo baseLineManagementVo) {
        LambdaQueryWrapper<BaseLineManagement> lw = new LambdaQueryWrapper<BaseLineManagement>();
        if (!StringUtils.isBlank(baseLineManagementVo.getLineName())) {
            lw.eq(BaseLineManagement::getLineName, baseLineManagementVo.getLineName());
        }
        if (!ObjectUtils.isEmpty(baseLineManagementVo.getLineType())) {
            lw.like(BaseLineManagement::getLineType, "%" + baseLineManagementVo.getLineType() + "%");
        }
        IPage<BaseLineManagement> page = baseLineManagementService.page(new Page<BaseLineManagement>(baseLineManagementVo.getPageNum(), baseLineManagementVo.getPageSize()), lw);
        return AjaxResult.success(page);
    }

    /**
     * 删除线路台账信息
     *
     * @param lineIds
     * @return
     */
    @DeleteMapping("/{lineIds}")
    @ApiOperation("删除线路台账信息")
    public AjaxResult removeLineManagement(@PathVariable List<Long> lineIds) {
        if (baseLineManagementService.removeByIds(lineIds)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 新增线路台账信息
     *
     * @param baseLineManagement
     * @return
     */
    @PostMapping("/saveLineManagement")
    @ApiOperation("新增线路台账信息")
    public AjaxResult saveLineManagement(@RequestBody BaseLineManagement baseLineManagement) {
        baseLineManagement.setCreateBy(getUsername());
        if (baseLineManagementService.save(baseLineManagement)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改线路台账信息
     *
     * @param baseLineManagement
     * @return
     */
    @PutMapping("/editLineManagement")
    @ApiOperation("修改线路台账信息")
    public AjaxResult editLineManagement(@RequestBody BaseLineManagement baseLineManagement) {
        baseLineManagement.setUpdateBy(getUsername());
        if (baseLineManagementService.updateById(baseLineManagement)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping(value = "/{lineId}")
    @ApiOperation("根据台账ID获取详细信息")
    public AjaxResult getInfo(@PathVariable Long lineId) {
        return AjaxResult.success(baseLineManagementService.getById(lineId));
    }

    /**
     * 线路台账导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @ApiOperation("线路台账导入")
    @PostMapping("/importBaseLineManagement")
    public AjaxResult importBaseLineManagement(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BaseLineManagement> util = new ExcelUtil<BaseLineManagement>(BaseLineManagement.class);
        List<BaseLineManagement> AccountManageList = util.importExcel(file.getInputStream());
        String message = baseLineManagementService.importBaseLineManagement(AccountManageList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导出线路台账
     *
     * @param response
     */
    @ApiOperation("导出线路台账")
    @PostMapping("/exportBaseLineManagement")
    public void exportBaseLineManagement(HttpServletResponse response, @RequestBody BaseLineManagementVo baseLineManagementVo) {
        LambdaQueryWrapper<BaseLineManagement> lw = new LambdaQueryWrapper<BaseLineManagement>();
        if (!StringUtils.isBlank(baseLineManagementVo.getLineName())) {
            lw.eq(BaseLineManagement::getLineName, baseLineManagementVo.getLineName());
        }
        if (!ObjectUtils.isEmpty(baseLineManagementVo.getLineType())) {
            lw.like(BaseLineManagement::getLineType, "%" + baseLineManagementVo.getLineType() + "%");
        }
        List<BaseLineManagement> list = baseLineManagementService.list(lw);
        ExcelUtil<BaseLineManagement> util = new ExcelUtil<>(BaseLineManagement.class);
        util.exportExcel(response, list, "线路台账数据");
    }

    @ApiOperation("模板下载")
    @GetMapping("/downloaExcel")
    public void downloadZip(HttpServletResponse response) {
        try {
            InputStream resourceAsStream = this.getClass().getResourceAsStream("/template/planQuarter.xlsx");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, "台账线路管理导入模板.xlsx");
            FileUtils.writeBytesByInput(resourceAsStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }
}

