package jnpf.personnels.controller.app.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 jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.entity.StoreEntity;
import jnpf.model.personnels.dto.staff.transfer.TransferPositionCountDto;
import jnpf.model.personnels.dto.staff.transfer.TransferPositionDto;
import jnpf.model.personnels.req.employment.EmploymentApplyCheckDto;
import jnpf.model.personnels.req.transfer.AppQueryTransferListReq;
import jnpf.model.personnels.req.transfer.SaveTransferReq;
import jnpf.model.personnels.vo.app.FtbPersonnelsBubbleCountVO;
import jnpf.permission.OrganizeApi;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.personnels.service.FtbPersonnelsStaffTransferPositionService;
import jnpf.store.service.StoreService;
import jnpf.util.FtbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @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 AppQueryTransferListReq req) {
        PageInfo<TransferPositionDto> pageVo = transferPositionService.getAppPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 我的审批状态数量统计
     *
     * @param
     * @return {@link ActionResult}<{@link TransferPositionDto}>
     */
    @GetMapping("/query-myCheck-count")
    public ActionResult<TransferPositionCountDto> queryMyCheckCount() {
        TransferPositionCountDto dto = transferPositionService.queryAppMyCheckCount();
        return ActionResult.success("成功", dto);
    }

    /**
     * 我的审批,抄送,小气泡统计数
     * @param flag "1" 我的审批 "2" 抄送
     */
    @GetMapping("/getListCont")
    public ActionResult<FtbPersonnelsBubbleCountVO> getListCont(@RequestParam String flag) {
        FtbPersonnelsBubbleCountVO  countVO = transferPositionService.getListCont(flag);
        return ActionResult.success(countVO);
    }

    /**
     * 我的抄送状态数量统计
     *
     * @param
     * @return {@link ActionResult}<{@link TransferPositionDto}>
     */
    @GetMapping("/query-mySend-count")
    public ActionResult<TransferPositionCountDto> queryMySendCount() {
        TransferPositionCountDto dto = transferPositionService.queryAppMySendCount();
        return ActionResult.success("成功", dto);
    }


    /**
     * 办理调岗
     *
     * @param req 调岗参数
     * @return  v1.1 返回 1 标识为门店负责人,需要进行替换
     */
    @PostMapping("/deal-transfer")
    public ActionResult<String> dealTransfer(@Validated @RequestBody SaveTransferReq req) {
        String string = transferPositionService.insertData(req);
        return ActionResult.success("",string);
    }
    /**
     * 查看调岗详细
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ActionResult<TransferPositionDto> get(@PathVariable("id") String id) {
        TransferPositionDto info = transferPositionService.getInfo(id);
        return ActionResult.success(info);
    }
    /**
     * 查看调岗详细
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public ActionResult<TransferPositionDto> getWithId(@RequestParam("id") String id) {
        TransferPositionDto info = transferPositionService.getInfo(id);
        return ActionResult.success(info);
    }

    /**
     * 审批
     *
     * @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();
    }


    /**
     * 重新审批
     *
     * @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);
    }
}
