package com.liang.service.impl;

import com.liang.entity.SysUserPost;
import com.liang.dao.SysUserPostDao;
import com.liang.service.SysUserPostService;
import com.liang.vo.PostXmSelect;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SysUserPost)表服务实现类
 *
 * @author makejava
 * @since 2023-03-01 19:44:20
 */
@Service("sysUserPostService")
public class SysUserPostServiceImpl implements SysUserPostService {
    @Resource
    private SysUserPostDao sysUserPostDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public SysUserPost queryById(Integer userId) {
        return this.sysUserPostDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param sysUserPost 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysUserPost> queryByPage(SysUserPost sysUserPost, PageRequest pageRequest) {
        long total = this.sysUserPostDao.count(sysUserPost);
        return new PageImpl<>(this.sysUserPostDao.queryAllByLimit(sysUserPost, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysUserPost 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserPost insert(SysUserPost sysUserPost) {
        this.sysUserPostDao.insert(sysUserPost);
        return sysUserPost;
    }

    /**
     * 修改数据
     *
     * @param sysUserPost 实例对象
     * @return 实例对象
     */
    @Override
    public SysUserPost update(SysUserPost sysUserPost) {
        this.sysUserPostDao.update(sysUserPost);
        return this.queryById(sysUserPost.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.sysUserPostDao.deleteById(userId) > 0;
    }

    @Override
    public Map<String, Object> listPostByUserId(Integer userId) {
        List<PostXmSelect> list;
//        if (StringUtils.isBlank(userId)) {
//            list = sysUserPostDao.listAllPost();
//        } else {
//            list = sysUserPostDao.listPostByUserId(userId);
//        }
        list = sysUserPostDao.listPostByUserId(userId);
        Map<String, Object> selectMap = new HashMap<>();
        selectMap.put("data", list);
        return selectMap;
    }

    @Override
    public int userPostBind(Integer userId, @RequestParam(required = false, defaultValue = "", value = "account") String postStrs) {
        // 先全部删除用户旧的岗位
        int num = this.sysUserPostDao.deleteUserPostByUserId(userId);

        // 保存用户新赋予的岗位
        if (StringUtils.isNotBlank(postStrs)) {
            String[] postArr = postStrs.split(",");
            List<SysUserPost> list = new ArrayList<>();
            SysUserPost userPost = null;
            for (String item : postArr) {
                userPost = new SysUserPost();
                userPost.setUserId(userId);
                userPost.setPostId(Integer.parseInt(item));
                list.add(userPost);
            }
            int numm = this.sysUserPostDao.batchSaveUserPost(list);
            return numm;
        } else {
            return 0;
        }
    }

    @Override
    public List<SysUserPost> getPostUserList(Map<String, String> paraMap) {
        return this.sysUserPostDao.getPostUserList(paraMap);
    }

    @Override
    public List<SysUserPost> getUserPostList(Map<String, String> paraMap) {
        return this.sysUserPostDao.getUserPostList(paraMap);
    }

    @Override
    public List<SysUserPost> getPostId(Integer userId) {
        return this.sysUserPostDao.getPostId(userId);
    }
}
