package com.sunxung.factoring.service.ledger.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ledger.LedgerAccountBasicInfo;
import com.sunxung.factoring.entity.ledger.vo.SupplierListVo;
import com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask;
import com.sunxung.factoring.entity.sys.Org;
import com.sunxung.factoring.entity.sys.Permission;
import com.sunxung.factoring.entity.sys.UserOrgRel;
import com.sunxung.factoring.mapper.ledger.LedgerAccountBasicInfoMapper;
import com.sunxung.factoring.service.customerinformation.CustomerService;
import com.sunxung.factoring.service.ledger.LedgerAccountBasicInfoService;
import com.sunxung.factoring.service.ledger.LedgerFundGoodsAssignService;
import com.sunxung.factoring.service.sys.OrgService;
import com.sunxung.factoring.service.sys.PermissionService;
import com.sunxung.factoring.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2021.11.11
 */
@Service
public class LedgerAccountBasicInfoServiceImpl extends ServiceImpl<LedgerAccountBasicInfoMapper, LedgerAccountBasicInfo>
        implements LedgerAccountBasicInfoService {

    @Autowired
    private UserService userService;

    @Autowired
    private OrgService orgService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public GridPage<LedgerAccountBasicInfo> list(SearchBusinessProcessingTask search) {
        Long currentUserId = ThreadLocalUtil.getUserId();
        List<UserOrgRel> userOrgRelList = userService.findOrgRelByUserId(currentUserId);

        Boolean flag = true;
        List<Long> userIdList = new ArrayList<>();
        for (UserOrgRel userOrgRel : userOrgRelList) {
            Org org = orgService.get(userOrgRel.getOrgId());
            if (org != null && org.getDeptNo().equals(ConstantConversionUtil.dept_no)) {
                flag = true;
                break;
            } else {
                flag = false;
                if (userOrgRel.isUserIdentity()) {
                    // 如果是部门负责人，获取部门下所有人员Id
                    List<Long> userIds = userService.findUserIdByOrgId(userOrgRel.getOrgId());
                    userIdList.addAll(userIds);
                } else {
                    userIdList.add(currentUserId);
                }
            }
        }
        //获取当前用户所有权限
        List<Long> pers = permissionService.getPermissionIdByUserId(ThreadLocalUtil.getUserId());
        //获取查询全员权限
        Permission per = permissionService.getByEname(ConstantConversionUtil.VIEW_ALL_LEDGER_BUSINESS);

        if (pers.contains(per.getId())
                || ThreadLocalUtil.getUser().getLoginName().equals(ConstantConversionUtil.ADMINISTORIES)) {
            flag = true;
        }
        if (!flag) {
            search.setUserIds(userIdList);
        }
        List<LedgerAccountBasicInfo> rows;
        Page<LedgerAccountBasicInfo> page = PageHelper.startPage(search.getPage(), search.getRows());
        rows = baseMapper.findAll(search);
        return new GridPage<>(search.getRows(), search.getPage(), page.getTotal(), rows);
    }

    @Override
    public List<SupplierListVo> getSupplierList(String businessNumber) {
        return baseMapper.getSupplierList(businessNumber);
    }

    @Override
    public LedgerAccountBasicInfo getLatestByManageIdAndNotApproval(Long manageId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ledger_account_management_id", manageId);
        queryWrapper.ne("is_effective", 1);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("LIMIT 1");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public LedgerAccountBasicInfo getDetail(Long id) {
        return baseMapper.getDetail(id);
    }

    @Override
    public List<LedgerAccountBasicInfo> findByManagementIdsAndEffective(List<Long> managementIds, Integer isEffective) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("ledger_account_management_id", managementIds);
        queryWrapper.ne("is_effective", isEffective);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public LedgerAccountBasicInfo findByVisable(Long manageId, Boolean visible) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ledger_account_management_id", manageId);
        queryWrapper.eq("visible", visible);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("LIMIT 1");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public LedgerAccountBasicInfo getLatestById(Long managementId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ledger_account_management_id", managementId);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("LIMIT 1");
        return baseMapper.selectOne(queryWrapper);
    }
}
