package com.jeeplus.business.drive.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.business.drive.service.DriveAccountService;
import com.jeeplus.business.drive.service.DriveApplyService;
import com.jeeplus.business.drive.service.dto.DriveAccountDTO;
import com.jeeplus.business.drive.service.dto.DriveApplyDTO;
import com.jeeplus.business.drive.service.dto.DriveDTO;
import com.jeeplus.business.finance.util.EasyPoiUtil;
import com.jeeplus.business.meetingRoom.service.dto.MeetingRoomDTO;
import com.jeeplus.business.monthly.service.dto.MonthlyDTO;
import com.jeeplus.common.TokenProvider;
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.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags ="用车申请及结算管理")
@RestController
@RequestMapping(value = "/drive")
public class DriveController {

    @Resource
    private DriveAccountService driveAccountService;

    @Resource
    private DriveApplyService driveApplyService;


    /**
     * 用车申请列表
     */
    @ApiOperation(value = "用车申请列表")
    @GetMapping(value = "/applyList")
    public ResponseEntity<IPage<DriveApplyDTO>> applyList(DriveApplyDTO driveApplyDTO, Page<DriveApplyDTO> page) throws Exception {
        IPage<DriveApplyDTO> pageList=driveApplyService.findPageList(driveApplyDTO,page);
        return ResponseEntity.ok(pageList);
    }

    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增或修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody DriveApplyDTO driveApplyDTO){
        String id=driveApplyService.save(driveApplyDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_drive_apply").add("businessId",id).ok("操作成功");
    }


    /**
     * 根据id查找用车申请信息
     */
    @ApiOperation(value = "根据id查找用车申请")
    @GetMapping(value = "findById")
    public ResponseEntity<DriveApplyDTO> findById(@RequestParam String id){
        DriveApplyDTO applyDTO=driveApplyService.findById(id);
        return ResponseEntity.ok(applyDTO);
    }

    /**
     * 根据id修改状态status
     * @param
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateStatusById")
    public void updateStatusById(@RequestBody DriveApplyDTO driveApplyDTO) {
        driveApplyService.updateStatusById(driveApplyDTO);
    }

    /**
     * 根据id进行删除
     */
    @ApiOperation(value = "根据id进行删除")
    @DeleteMapping(value = "/delete")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = driveApplyService.deleteById(id);
        return ResponseEntity.ok(s);
    }

    /**
     * 新增/修改  用车结算
     */
    @ApiOperation(value = "新增或修改 用车结算")
    @PostMapping(value = "/saveAccountForm")
    public ResponseEntity saveAccountForm(@RequestBody DriveAccountDTO driveAccountDTO){
        String id=driveApplyService.saveAccountForm(driveAccountDTO);
        return ResponseUtil.newInstance().add("businessTable","jy_drive_account").add("businessId",id).ok("操作成功");
    }

    /**
     * 根据申请id修改状态status
     * @param
     */
    @ApiOperation(value = "根据申请id修改状态status")
    @PostMapping(value = "/updateStatusByApplyId")
    public void updateStatusByApplyId(@RequestBody DriveAccountDTO driveAccountDTO) {
        driveApplyService.updateStatusByApplyId(driveAccountDTO);
    }


    /**
     * 管理员修改
     */
    @ApiOperation(value = "管理员修改")
    @PostMapping(value = "/adminEditForm")
    public ResponseEntity adminEditForm(@RequestBody DriveDTO driveDTO){
        driveApplyService.adminEditForm(driveDTO);
        return ResponseEntity.ok("操作成功");
    }

    @ApiOperation(value = "校验用车人员是否是本部门员工")
    @GetMapping(value = "/checkUseMen")
    public boolean checkUseMen(@RequestParam String useMen){
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByToken(TokenProvider.getCurrentToken());
        //判断用车人员是否是多个
        ArrayList<UserDTO> userDTOS = new ArrayList<>();
        if (useMen.contains(",")){
            //获取到用车人员的信息
            String[] split = useMen.split(",");
            for (String s : split) {
                UserDTO dto = SpringUtil.getBean(IUserApi.class).getById(s);
                userDTOS.add(dto);
            }
            //判断与当前登陆人是否是同部门
            for (UserDTO dto : userDTOS) {
                if (!dto.getOfficeDTO().getId().equals(userDTO.getOfficeDTO().getId())){
                    return false;
                }
            }
        }else {
            UserDTO dto = SpringUtil.getBean(IUserApi.class).getById(useMen);
            if (!dto.getOfficeDTO().getId().equals(userDTO.getOfficeDTO().getId())){
                return false;
            }
        }
        return true;
    }


    @ApiLog(value = "导出用车申请数据", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    @ApiOperation(value = "导出用车申请数据")
    public void exportFile(DriveApplyDTO driveApplyDTO, Page <DriveApplyDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<DriveApplyDTO> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = driveApplyService.findPageList(driveApplyDTO,page).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = driveApplyService.findPageList(driveApplyDTO,page).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = driveApplyService.findPageList(driveApplyDTO,page).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, DriveApplyDTO.class, fileName, response );

    }











}
