package com.jeeplus.test.changes.dimission.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.changes.dimission.domain.ChangesDimission;
import com.jeeplus.test.changes.dimission.domain.ChangesDimissionHandover;
import com.jeeplus.test.changes.dimission.service.ChangesDimissionHandoverInitializeService;
import com.jeeplus.test.changes.dimission.service.ChangesDimissionHandoverService;
import com.jeeplus.test.changes.dimission.service.ChangesDimissionService;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionHandoverDTO;
import com.jeeplus.test.changes.dimission.service.dto.ChangesDimissionHandoverInitializeDTO;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionHandoverWrapper;
import com.jeeplus.test.changes.dimission.service.mapstruct.ChangesDimissionWrapper;
import com.jeeplus.test.roster.domain.RosterBase;
import com.jeeplus.test.roster.service.RosterBaseService;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api("异动管理-离职交接")
@RestController
@RequestMapping(value = "/changes/dimission_handover")
public class ChangesDimissionHandoverController {

    @Resource
    private ChangesDimissionService changesDimissionService;
    @Resource
    private RosterBaseService rosterBaseService;
    @Resource
    private ChangesDimissionHandoverService changesDimissionHandoverService;
    @Resource
    private ChangesDimissionHandoverInitializeService changesDimissionHandoverInitializeService;

    /**
     * 查询离职交接列表
     * @param changesDimissionHandoverDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询离职交接列表")
    @PreAuthorize("hasAuthority('changes:dimission:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<ChangesDimissionHandoverDTO>> data(ChangesDimissionHandoverDTO changesDimissionHandoverDTO, Page<ChangesDimissionHandoverDTO> page) throws Exception {
        ChangesDimissionHandover changesDimissionHandover = ChangesDimissionHandoverWrapper.INSTANCE.toEntity(changesDimissionHandoverDTO);
        QueryWrapper<ChangesDimissionHandover> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( changesDimissionHandover, ChangesDimissionHandover.class );
        IPage<ChangesDimissionHandoverDTO> result = changesDimissionHandoverService.findList (page,queryWrapper);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询离职数据
     * @param id
     * @return
     */
    @ApiLog("查询离职交接详情")
    @PreAuthorize ("hasAnyAuthority('changes:dimission:view','changes:dimission:add','changes:dimission:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        List<ChangesDimissionHandoverDTO> changesDimissionHandoverDTOList = changesDimissionHandoverService.queryByCdId ( id );
        return ResponseEntity.ok (changesDimissionHandoverDTOList);
    }

    /**
     * 查询离职交接初始值数据
     * @return
     */
    @ApiLog("查询离职交接初始值数据")
    @GetMapping("queryInitialize")
    public ResponseEntity queryInitialize() {
        List<ChangesDimissionHandoverInitializeDTO> changesDimissionHandoverDTOList = changesDimissionHandoverInitializeService.getList ();
        return ResponseEntity.ok (changesDimissionHandoverDTOList);
    }

    /**
     * 保存离职交接信息
     * @param changesDimissionHandoverDTO
     * @return
     */
    @ApiLog(value = "修改/新增离职交接信息", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('changes:dimission:save','changes:dimission:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody ChangesDimissionHandoverDTO changesDimissionHandoverDTO) {
        changesDimissionHandoverService.saveHandover(changesDimissionHandoverDTO);
        return ResponseUtil.newInstance().add("businessTable", "changes_dimission").add("businessId", changesDimissionHandoverDTO.getCdId()).ok ("操作成功");
    }

    /**
     * 同意、驳回、撤回   离职交接审批
     * @param changesDimissionDTO
     */
    @ApiLog(value = "同意/驳回/撤回离职交接审批", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('changes:dimission:save','changes:dimission:edit')")
    @PostMapping("updateByCdId")
    public void updateByCdId(@Valid @RequestBody ChangesDimissionDTO changesDimissionDTO) {
        changesDimissionHandoverService.updateByCdId(changesDimissionDTO);
    }

}
