package org.dromara.zhishu.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.NewUserInfo;
import org.dromara.zhishu.domain.TAudit;
import org.dromara.zhishu.domain.vo.TAuditVo;
import org.dromara.zhishu.mapper.TAuditMapper;
import org.springframework.stereotype.Service;
import org.dromara.zhishu.domain.bo.UserTAuditBo;
import org.dromara.zhishu.domain.vo.UserTAuditVo;
import org.dromara.zhishu.domain.UserTAudit;
import org.dromara.zhishu.mapper.UserTAuditMapper;
import org.dromara.zhishu.service.IUserTAuditService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 列表Service业务层处理
 *
 * @author Lion Li
 * @date 2025-04-02
 */
@RequiredArgsConstructor
@Service
public class UserTAuditServiceImpl implements IUserTAuditService {

    private final UserTAuditMapper baseMapper;
    private final TAuditMapper  userTAuditMapper;

    /**
     * 查询列表
     *
     * @param id 主键
     * @return 列表
     */
    @Override
    public UserTAuditVo queryById(Long id){
        NewUserInfo infor=baseMapper.selectU(id);
        // 根据audit_id查询出t_audit_info表中数据
        UserTAuditVo tAuditVo=baseMapper.selectM(infor.getAuditId());
        // 赋值给tAuditVo
        tAuditVo.setId(infor.getId());
        tAuditVo.setCompanyName(infor.getCompanyName());
        tAuditVo.setCompanyType(infor.getCompanyType());
        tAuditVo.setContactPerson(infor.getContactPerson());
        tAuditVo.setContactPhone(infor.getContactPhone());
        tAuditVo.setEmail(infor.getEmail());
        tAuditVo.setLicense(infor.getLicense());
        tAuditVo.setRemark(infor.getRemark());
        tAuditVo.setCardIdentity(infor.getCardIdentity());
        tAuditVo.setLicenseName(infor.getLicenseName());
        tAuditVo.setLicenseNumber(infor.getLicenseNumber());
        tAuditVo.setAdress(infor.getAdress());
        tAuditVo.setBusinessLicense(infor.getBusinessLicense());
        tAuditVo.setLicenseTime(infor.getLicenseTime());
        tAuditVo.setBusinessLicenseTime(infor.getBusinessLicenseTime());
        return tAuditVo;

    }

    /**
     * 分页查询列表列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 列表分页列表
     */
    @Override
    public TableDataInfo<UserTAuditVo> queryPageList(UserTAuditBo bo, PageQuery pageQuery) {
        // 获取当前用户id
        Long userId = LoginHelper.getUserId();
        // 根据用户id查询出t_audit_info表中对应id
        Long id= baseMapper.selectId(userId);
        // 判断id是否为空
        if(id==null){
            return TableDataInfo.build();
        }
        //把id和userId赋值给bo，并传入baseMapper
        bo.setId(id);
        bo.setUserId(userId);
        //自定义分页查询
        Page<UserTAuditVo> result = baseMapper.customPageList(pageQuery.build(), bo);

        // 遍历result，把id和userId赋值给result
        for (int i = 0; i < result.getRecords().size(); i++) {
            Long ids = result.getRecords().get(i).getId();
            String status = baseMapper.selectStatus(id);
            result.getRecords().get(i).setId(ids);
            result.getRecords().get(i).setStatus(status);
            String name = baseMapper.selectUserName(userId);
            NewUserInfo info = baseMapper.selectUserList(ids);
            result.getRecords().get(i).setCompanyName(info.getCompanyName());
            result.getRecords().get(i).setCompanyType(info.getCompanyType());
            result.getRecords().get(i).setContactPerson(info.getContactPerson());
            result.getRecords().get(i).setContactPhone(info.getContactPhone());
            result.getRecords().get(i).setEmail(info.getEmail());
            result.getRecords().get(i).setLicense(info.getLicense());
            result.getRecords().get(i).setRemark(info.getRemark());
            result.getRecords().get(i).setUserName(name);
        }

        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的列表列表
     *
     * @param bo 查询条件
     * @return 列表列表
     */
    @Override
    public List<UserTAuditVo> queryList(UserTAuditBo bo) {
        LambdaQueryWrapper<UserTAudit> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserTAudit> buildQueryWrapper(UserTAuditBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserTAudit> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, UserTAudit::getAdminId, bo.getId());
        return lqw;
    }

    /**
     * 新增列表
     *
     * @param bo 列表
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserTAuditBo bo) {
        UserTAudit add = MapstructUtils.convert(bo, UserTAudit.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
        }
        return flag;
    }

    /**
     * 修改列表
     *
     * @param bo 列表
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserTAuditBo bo) {
        bo.setStatus("2");
        UserTAudit update = MapstructUtils.convert(bo, UserTAudit.class);
        NewUserInfo newUserInfo=new NewUserInfo();
        newUserInfo.setCompanyName(bo.getCompanyName());
        newUserInfo.setCompanyType(bo.getCompanyType());
        newUserInfo.setContactPerson(bo.getContactPerson());
        newUserInfo.setContactPhone(bo.getContactPhone());
        newUserInfo.setEmail(bo.getEmail());
        newUserInfo.setLicense(bo.getLicense());
        newUserInfo.setRemark(bo.getRemark());
        newUserInfo.setId(update.getId());
        newUserInfo.setLicenseName(bo.getLicenseName());
        newUserInfo.setLicenseNumber(bo.getLicenseNumber());
        newUserInfo.setCardIdentity(bo.getCardIdentity());
        newUserInfo.setAdress(bo.getAdress());
        newUserInfo.setLicenseTime(bo.getLicenseTime());
        newUserInfo.setBusinessLicenseTime(bo.getBusinessLicenseTime());
        newUserInfo.setBusinessLicense(bo.getBusinessLicense());
        // 修改t_audit_info表中的数据
        userTAuditMapper.updateByUser(newUserInfo);
        //更新时间
        Boolean message=  baseMapper.updateUserId(update);
        return message;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(UserTAudit entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除列表信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
          Long auditId = baseMapper.selectAudit(ids);
          Integer message=baseMapper.deleteAudit(auditId);
          if(message==0){
              return false;
          }
         Long message1 = baseMapper.deleteByAudit(ids);
        return message1> 0;
    }

    @Override
    public UserTAuditVo getAuditLog() {
        Long userId = LoginHelper.getUserId();
       Long message= baseMapper.selectId(userId);
        return  baseMapper.getAuditLog(message);
    }
}
