package com.bonus.material.toolLedger.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.devchange.domain.MaDevInfo;
import com.bonus.material.devchange.domain.MaDevInfoXlsx;
import com.bonus.material.project.domain.Project;
import com.bonus.material.tool.domain.ToolEntity;
import com.bonus.material.toolLedger.domain.ToolLedgerAllEntity;
import com.bonus.material.toolLedger.domain.ToolLedgerEntity;
import com.bonus.material.toolLedger.service.ToolLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/toolLedger")
@Api(value = "工具台账", tags = "工具台账")
public class ToolLedgerController extends BaseController {

    @Resource
    private ToolLedgerService toolLedgerService;


    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具台账表格")
    @GetMapping("/list")
    public TableDataInfo list(ToolLedgerAllEntity entity) {
        try {
            startPage();
            List<ToolLedgerAllEntity> list = toolLedgerService.list(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具台账表格")
    @GetMapping("/listCode")
    public TableDataInfo listCode(ToolLedgerEntity entity) {
        try {
            startPage();
            List<ToolLedgerEntity> list = toolLedgerService.listCode(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具台账表格")
    @PostMapping("/updateById")
    public AjaxResult updateById(@RequestBody ToolLedgerEntity entity) {
        return toolLedgerService.updateById(entity);
    }


    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具台账表格")
    @GetMapping("/getToolByTypeId")
    public TableDataInfo getToolByTypeId(ToolLedgerEntity entity) {
        try {
            startPage();
            List<ToolLedgerEntity> list = toolLedgerService.getToolByTypeId(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具台账表格")
    @GetMapping("/getToolByPro")
    public TableDataInfo getToolByPro(ToolLedgerEntity entity) {
        try {
            startPage();
            List<ToolLedgerEntity> list = toolLedgerService.getToolByPro(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 工具台账表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具台账表格-共享")
    @GetMapping("/getToolByOrder")
    public TableDataInfo getToolByOrder(ToolLedgerEntity entity) {
        try {
            startPage();
            List<ToolLedgerEntity> list = toolLedgerService.getToolByOrder(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }



    /**
     * 导出
     * @param response
     * @param entity
     */
    @PostMapping("/export")
    @SysLog(title = "编码工具台账", businessType = OperaType.EXPORT, logType = 0, module = "编码工具台账", details = "编码工具台账")
    public void export(HttpServletResponse response, ToolLedgerEntity entity) {
        try {
            List<ToolLedgerEntity> list = toolLedgerService.listCode(entity);
            ExcelUtil<ToolLedgerEntity> util = new ExcelUtil<ToolLedgerEntity>(ToolLedgerEntity.class);
            util.exportExcel(response,list,"编码工具台账");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }

    /**
     * 导出
     * @param response
     * @param entity
     */
    @PostMapping("/exportAll")
    @SysLog(title = "工具台账", businessType = OperaType.EXPORT, logType = 0, module = "工具台账", details = "工具台账")
    public void export(HttpServletResponse response, ToolLedgerAllEntity entity) {
        try {
            List<ToolLedgerAllEntity> list = toolLedgerService.list(entity);
            list.stream()
                    .forEach(item -> {
                        String code = item.getManageMode();
                        String name = "0".equals(code) ? "编码工具" : "1".equals(code) ? "数量工具" : "未知管理模式";
                        item.setManageMode(name);
                    });
            ExcelUtil<ToolLedgerAllEntity> util = new ExcelUtil<ToolLedgerAllEntity>(ToolLedgerAllEntity.class);
            util.exportExcel(response,list,"工具台账");
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
    }


}
