package jnpf.personnels.controller.web.transfer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import io.seata.spring.annotation.GlobalTransactional;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.entity.StoreEntity;
import jnpf.model.personnels.dto.staff.transfer.TransferPositionDto;
import jnpf.model.personnels.req.employment.EmploymentApplyCheckDto;
import jnpf.model.personnels.req.transfer.MyWebTransferCheckListReq;
import jnpf.model.personnels.req.transfer.QueryTransferListReq;
import jnpf.model.personnels.req.transfer.SaveTransferReq;
import jnpf.permission.OrganizeApi;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.personnels.service.FtbPersonnelsStaffTransferPositionService;
import jnpf.store.service.StoreService;
import jnpf.util.FtbUtil;
import jnpf.util.excel.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * web员工调岗表模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/web/personnels-staff-transfer-position")
public class FtbPersonnelsStaffTransferPositionController {

    @Autowired
    private FtbPersonnelsStaffTransferPositionService transferPositionService;


    @Resource
    StoreService storeService;

    @Autowired
    OrganizeApi organizeApi;

    /**
     * 已调岗员工列表/调岗审批列表
     *
     * @param req
     * @return {@link ActionResult}<{@link TransferPositionDto}>
     */
    @GetMapping("/query-list")
    public ActionResult<PageListVO<TransferPositionDto>> pageLists(@Validated QueryTransferListReq req) {
        PageInfo<TransferPositionDto> pageVo = transferPositionService.getPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 调岗员工列表导出
     *
     * @param req
     * @return {@link ActionResult}<{@link TransferPositionDto}>
     */
    @GetMapping("/query-list-export")
    public void pageListsExport(@Validated QueryTransferListReq req, HttpServletResponse response) throws IOException {
        req.setPageSize(Integer.MAX_VALUE);
        PageInfo<TransferPositionDto> pageVo = transferPositionService.getPageList(req);
        pageVo.getList().parallelStream().forEach(a -> {
            // 状态
            TransferPositionDto.changeApprovalStatus(a);
            // 试用期
            TransferPositionDto.probationStatus(a);
        });
        String fileName = "调岗信息";
        EasyExcelUtils.exportExcel(response, fileName, pageVo.getList(), TransferPositionDto.class);
    }

    /**
     * 我的审批（已审批/待审批）
     *
     * @param req
     * @return {@link ActionResult}<{@link TransferPositionDto}>
     */
    @GetMapping("/query-myCheck-list")
    @Deprecated(since = "人事v1.2接入oa废弃")
    public ActionResult<PageListVO<TransferPositionDto>> queryMycheckList(@Validated MyWebTransferCheckListReq req) {
        PageInfo<TransferPositionDto> pageVo = transferPositionService.pageQueryMyCheckList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 办理调岗
     *
     * @param req 调岗参数
     * @return v1.1 返回 1 标识为门店负责人,需要进行替换 5 返回薪酬冲突
     */
    @PostMapping("/deal-transfer")
    public ActionResult<String> dealTransfer(@Validated @RequestBody SaveTransferReq req) {
        String string = transferPositionService.insertData(req);
        return ActionResult.success("成功",string);
    }

    /**
     * 办理调岗(for oa) 包含重新办理
     *
     * @param req 调岗参数
     * @return
     */
    @PostMapping("/deal-transfer-for-oa")
    public ActionResult<Void> dealTransferForOA(@Validated @RequestBody SaveTransferReq req) {
        ActionResult<Void> map = transferPositionService.dealTransferForOA(req);
        return map;
    }


    /**
     * 查看调岗详细
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ActionResult<TransferPositionDto> get(@PathVariable("id") String id) {
        TransferPositionDto info = transferPositionService.getInfo(id);
        return ActionResult.success(info);
    }
    /**
     * 查看调岗详细(for oa)
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ActionResult<TransferPositionDto> getWith(@RequestParam("id") String id) {
        TransferPositionDto info = transferPositionService.getInfo(id);
        return ActionResult.success(info);
    }

    /**
     * 删除调岗记录
     *
     * @param id 主键id，必传
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        transferPositionService.deleteData(id);
        return ActionResult.success();
    }

    /**
     * 审批
     *
     * @param id 主键id，必传
     * @return
     */
    @PutMapping("/approval/{id}")
    public ActionResult approval(@PathVariable("id") String id, @RequestBody EmploymentApplyCheckDto dto) {
        dto.setId(id);
        transferPositionService.approval(dto);
        return ActionResult.success();
    }


    /**
     * 审批(for oa)
     *
     */
    @PostMapping("/approval/id")
    @GlobalTransactional
    public ActionResult<Void> approvalForOA(@RequestBody EmploymentApplyCheckDto dto) {
        return transferPositionService.approvalForOA(dto);
    }


    /**
     * 重新办理
     *
     * @param id 主键id，必传
     * @return
     */

    @PutMapping("/re-approval/{id}")
    public ActionResult reApproval(@PathVariable("id") String id, @RequestBody SaveTransferReq dto) {
        transferPositionService.reApproval(id,dto);
        return ActionResult.success();
    }

    /**
     * 获取门店负责人详情
     * @param userId 门店负责人id
     * @return organizeName 组织名称   storeName   门店名称
     */
    @GetMapping("/getStoreManagerInfo")
    public ActionResult<List<Map<String,Object>>> getStoreManagerInfo(@RequestParam("userId") String userId) {
        LambdaQueryWrapper<StoreEntity> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(StoreEntity::getStoreheaduserid,userId);
        List<StoreEntity> list = storeService.list(objectLambdaQueryWrapper);
        List<Map<String, Object>> maps = list.stream().map(item -> {
            Map<String, Object> map = Maps.newHashMap();
            OrganizeEntity organize = organizeApi.getInfoById(item.getOrganizeid());
            map.put("organizeName", organize.getFullName());
            map.put("storeName", item.getStorename());
            return map;
        }).collect(Collectors.toList());
        return ActionResult.success(maps);
    }
}
