package com.tofly.feesapi.bmgl.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bmgl.entity.BmCbb;
import com.tofly.feesapi.bmgl.entity.BmCbpq;
import com.tofly.feesapi.bmgl.entity.dto.BmCbbFilter;
import com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect;
import com.tofly.feesapi.bmgl.entity.dto.BmCbbSelect2;
import com.tofly.feesapi.bmgl.mapper.BmCbbMapper;
import com.tofly.feesapi.bmgl.service.BmCbbService;
import com.tofly.feesapi.bmgl.service.BmCbpqService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.udb.entity.Company;
import com.tofly.feesapi.udb.entity.User;
import com.tofly.feesapi.udb.service.ICompanyService;
import com.tofly.feesapi.udb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("bmCbbService")
public class BmCbbServiceImpl extends ServiceImpl<BmCbbMapper, BmCbb> implements BmCbbService {

    @Autowired
    private IUserService userService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private BmCbpqService bmCbpqService;

    @Override
    public PageResult<BmCbbSelect> getPage(PageDto pageDto, BmCbbFilter filter) {
        if (StringUtil.isEmpty(filter.getUserId()) && !"0".equals(filter.getIsqysjqx())) {
            filter.setUserId(SecurityUtils.getUserId().toString());
        }
        PageDto<BmCbbSelect> page = this.baseMapper.getPage(pageDto, filter);

        List<User> userList = userService.getAllUser();
        List<Company> companyList = companyService.list();
        page.getRecords().forEach(e -> {
            if (e.getCby() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getCby()))).findFirst().orElse(null);
                if (user != null) {
                    e.setCbymc(user.getRealName());
                } else {
                    e.setCbymc(e.getCby());
                }
            }
            if (StringUtil.isNotEmpty(e.getQyfzr())) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getQyfzr()))).findFirst().orElse(null);
                if (user != null) {
                    e.setQyfzrmc(user.getRealName());
                } else {
                    e.setQyfzrmc(e.getCby());
                }
            }
            if (e.getXycby() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getXycby()))).findFirst().orElse(null);
                if (user != null) {
                    e.setXycbymc(user.getRealName());
                } else {
                    e.setXycbymc(e.getXycby());
                }
            }
            if (e.getSsgs() != null) {
                Company company = companyList.stream().filter(item -> item.getId().equals(Long.valueOf(e.getSsgs()))).findFirst().orElse(null);
                if (company != null) {
                    e.setSsgsmc(company.getCompanyName());
                } else {
                    e.setSsgsmc(e.getSsgs());
                }
            }

        });
        return page.toVo();
    }

    @Override
    public List<BmCbbSelect> cbblist(BmCbbFilter filter) {
        List<BmCbbSelect> list = this.baseMapper.getPage(filter);
        List<User> userList = userService.getAllUser();
        List<Company> companyList = companyService.list();
        list.forEach(e -> {
            if (e.getCby() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getCby()))).findFirst().orElse(null);
                if (user != null) {
                    e.setCbymc(user.getRealName());
                } else {
                    e.setCbymc(e.getCby());
                }
            }
            if (StringUtil.isNotEmpty(e.getQyfzr())) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getQyfzr()))).findFirst().orElse(null);
                if (user != null) {
                    e.setQyfzrmc(user.getRealName());
                } else {
                    e.setQyfzrmc(e.getCby());
                }
            }
            if (e.getXycby() != null) {

                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(e.getXycby()))).findFirst().orElse(null);
                if (user != null) {
                    e.setXycbymc(user.getRealName());
                } else {
                    e.setXycbymc(e.getXycby());
                }
            }
            if (e.getSsgs() != null) {
                Company company = companyList.stream().filter(item -> item.getId().equals(Long.valueOf(e.getSsgs()))).findFirst().orElse(null);
                if (company != null) {
                    e.setSsgsmc(company.getCompanyName());
                } else {
                    e.setSsgsmc(e.getSsgs());
                }
            }
        });
        return list;
    }

    @Override
    public PageResult<BmCbbSelect2> getPage2(PageDto pageDto, BmCbbFilter filter) {
        PageDto<BmCbbSelect2> page = baseMapper.getPage2(pageDto, filter);
        return page.toVo();
    }

    @Override
    public PageResult<BmCbbSelect2> getPage3(PageDto pageDto, BmCbbFilter filter) {
        PageDto<BmCbbSelect2> page = this.baseMapper.getPage3(pageDto, filter);
        return page.toVo();
    }

    /**
     * 根据抄表人员编码获取抄表册
     *
     * @param rybm
     * @return
     */
    public List<BmCbb> getListCbbByRybm(String rybm) {
        List<BmCbb> list = this.baseMapper.selectList(new QueryWrapper<BmCbb>().eq("cby", rybm));
        return list;
    }

    @Override
    public List<BmCbb> selectByBmList(List<String> cbbbmList) {
        if (CollectionUtil.isEmpty(cbbbmList)) {
            return new ArrayList<>();
        }

        LambdaQueryWrapper<BmCbb> queryWrapper = Wrappers.<BmCbb>lambdaQuery().in(BmCbb::getCbbbm, cbbbmList);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BmCbb getByIdNewly(String cbbbm) {
        BmCbb bmCbb = baseMapper.selectById(cbbbm);
        if (ObjUtil.isNotNull(bmCbb)) {
            List<User> userList = userService.getAllUser();

            if (bmCbb.getCby() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(bmCbb.getCby()))).findFirst().orElse(null);
                if (user != null) {
                    bmCbb.setCbymc(user.getRealName());
                } else {
                    bmCbb.setCbymc(bmCbb.getCby());
                }
            }
            if (StringUtil.isNotEmpty(bmCbb.getQyfzr())) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(bmCbb.getQyfzr()))).findFirst().orElse(null);
                if (user != null) {
                    bmCbb.setQyfzrmc(user.getRealName());
                } else {
                    bmCbb.setQyfzrmc(bmCbb.getCby());
                }
            }
            if (bmCbb.getXycby() != null) {
                User user = userList.stream().filter(u -> u.getId().equals(Long.valueOf(bmCbb.getXycby()))).findFirst().orElse(null);
                if (user != null) {
                    bmCbb.setXycbymc(user.getRealName());
                } else {
                    bmCbb.setXycbymc(bmCbb.getXycby());
                }
            }
            if (bmCbb.getSsgs() != null) {
                Company company = companyService.get(Long.valueOf(bmCbb.getSsgs()));
                if (company != null) {
                    bmCbb.setSsgsmc(company.getCompanyName());
                } else {
                    bmCbb.setSsgsmc(bmCbb.getSsgs());
                }
            }
            if (bmCbb.getSscbpq() != null) {
                String sscbpq = bmCbb.getSscbpq();
                BmCbpq bmCbpq = bmCbpqService.getById(sscbpq);
                if (ObjUtil.isNotNull(bmCbpq)) {
                    bmCbb.setCbpqmc(bmCbpq.getCbpqmc());
                }
            }

            return bmCbb;
        }
        return null;
    }

    @Override
    public List<BmCbb> selectList() {
        return baseMapper.selectList(new QueryWrapper<>());
    }


}
