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.BaseDynamicManagement;
import com.ozs.service.entity.vo.BaseAccountManagementVo;
import com.ozs.service.service.BaseAccountManagementService;
import com.ozs.service.service.BaseDynamicManagementService;
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/baseAccountManagement")
@Slf4j
public class BaseAccountManagementController extends BaseController {
    @Resource
    BaseAccountManagementService baseAccountManagementService;
    @Resource
    BaseDynamicManagementService baseDynamicManagementService;

    /**
     * 相机台账管理分页
     *
     * @param baseAccountManagementVo
     * @return
     */
    @ApiOperation(value = "相机台账管理分页")
    @PostMapping("/list")
    public AjaxResult selectHomeNotice(@RequestBody BaseAccountManagementVo baseAccountManagementVo) {
        LambdaQueryWrapper<BaseAccountManagement> lw = new LambdaQueryWrapper<BaseAccountManagement>();
        if (!StringUtils.isBlank(baseAccountManagementVo.getLine())) {
            lw.eq(BaseAccountManagement::getLine, baseAccountManagementVo.getLine());
        }
        if (!StringUtils.isBlank(baseAccountManagementVo.getLineType())) {
            lw.eq(BaseAccountManagement::getLineType, baseAccountManagementVo.getLineType());
        }
        if (!StringUtils.isBlank(baseAccountManagementVo.getPublicWorksSection())) {
            lw.eq(BaseAccountManagement::getPublicWorksSection, baseAccountManagementVo.getPublicWorksSection());
        }
        if (!ObjectUtils.isEmpty(baseAccountManagementVo.getBeginMile())) {
            lw.ge(BaseAccountManagement::getBeginMile, baseAccountManagementVo.getBeginMile());
        }
        if (!ObjectUtils.isEmpty(baseAccountManagementVo.getEndMile())) {
            lw.le(BaseAccountManagement::getEndMile, baseAccountManagementVo.getEndMile());
        }
        IPage<BaseAccountManagement> page = baseAccountManagementService.page(new Page<>(baseAccountManagementVo.getPageNum(), baseAccountManagementVo.getPageSize()), lw);
        return AjaxResult.success(page);
    }

    /**
     * 删除相机信息
     *
     * @param accountManagementIds
     * @return
     */
    @DeleteMapping("/{accountManagementIds}")
    @ApiOperation("删除相机信息")
    public AjaxResult removeAccountManagement(@PathVariable List<Long> accountManagementIds) {
        if (baseAccountManagementService.removeByIds(accountManagementIds)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 新增相机信息
     *
     * @param baseAccountManagement
     * @return
     */
    @PostMapping("/saveAccountManagement")
    @ApiOperation("新增相机信息")
    public AjaxResult saveDistributionModule(@RequestBody BaseAccountManagement baseAccountManagement) {
        baseAccountManagement.setCreateBy(getUsername());
        BaseDynamicManagement baseDynamicManagement = new BaseDynamicManagement();
        baseDynamicManagement.setCameraCode(baseAccountManagement.getCameraCode());
        if (baseAccountManagementService.save(baseAccountManagement) && baseDynamicManagementService.save(baseDynamicManagement)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改相机信息
     *
     * @param baseAccountManagement
     * @return
     */
    @PutMapping("/editAccountManagement")
    @ApiOperation("修改相机信息")
    public AjaxResult editAccountManagement(@RequestBody BaseAccountManagement baseAccountManagement) {
        baseAccountManagement.setUpdateBy(getUsername());
        if (baseAccountManagementService.updateById(baseAccountManagement)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping(value = "/{accountManagementId}")
    @ApiOperation("根据相机ID获取详细信息")
    public AjaxResult getInfo(@PathVariable Long accountManagementId) {
        return AjaxResult.success(baseAccountManagementService.getById(accountManagementId));
    }

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

    /**
     * 导出台账管理
     *
     * @param response
     */
    @ApiOperation("导出台账管理")
    @PostMapping("/exportBaseAccountManagement")
    public void exportBaseAccountManagement(HttpServletResponse response, @RequestBody BaseAccountManagementVo baseAccountManagementVo) {
        LambdaQueryWrapper<BaseAccountManagement> lw = new LambdaQueryWrapper<BaseAccountManagement>();
        if (!StringUtils.isBlank(baseAccountManagementVo.getLine())) {
            lw.eq(BaseAccountManagement::getLine, baseAccountManagementVo.getLine());
        }
        if (!StringUtils.isBlank(baseAccountManagementVo.getLineType())) {
            lw.eq(BaseAccountManagement::getLineType, baseAccountManagementVo.getLineType());
        }
        if (!StringUtils.isBlank(baseAccountManagementVo.getPublicWorksSection())) {
            lw.eq(BaseAccountManagement::getPublicWorksSection, baseAccountManagementVo.getPublicWorksSection());
        }
        if (!ObjectUtils.isEmpty(baseAccountManagementVo.getBeginMile())) {
            lw.ge(BaseAccountManagement::getBeginMile, baseAccountManagementVo.getBeginMile());
        }
        if (!ObjectUtils.isEmpty(baseAccountManagementVo.getEndMile())) {
            lw.le(BaseAccountManagement::getEndMile, baseAccountManagementVo.getEndMile());
        }
        List<BaseAccountManagement> list = baseAccountManagementService.list(lw);
        ExcelUtil<BaseAccountManagement> util = new ExcelUtil<>(BaseAccountManagement.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);
        }
    }
}

