package com.iotechn.unimall.admin.api.card.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.util.ExcelUtil;
import com.iotechn.unimall.data.mapper.PersonalHomepageInfoMapper;
import com.iotechn.unimall.data.domain.PersonalHomepageInfo;
import com.iotechn.unimall.admin.api.card.IPersonalHomepageInfoService;
import com.iotechn.unimall.data.model.Page;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

/**
 * 个人主页Service业务层处理
 *
 * @author jlb
 * @date 2023-04-10
 */
@Service
public class PersonalHomepageInfoServiceImpl implements IPersonalHomepageInfoService {
    @Autowired
    private PersonalHomepageInfoMapper personalHomepageInfoMapper;

    @Override
    public Boolean add(PersonalHomepageInfo personalHomepageInfo) throws ServiceException {
        Date now = new Date();
        personalHomepageInfo.setGmtCreate(now);
        personalHomepageInfo.setGmtUpdate(now);
        return personalHomepageInfoMapper.insert(personalHomepageInfo) > 0;
    }

    @Override
    public Page<PersonalHomepageInfo> list(Long commonId, String companyName, String companyIntroduction, String scopeOfBusiness, String companyAddress, String phone, String portraiture, String postbox, String addressUrl, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<PersonalHomepageInfo> wrapper = new EntityWrapper<PersonalHomepageInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(companyName)) {
            wrapper.eq("company_name", companyName);
        }
        if (!StringUtils.isEmpty(companyIntroduction)) {
            wrapper.eq("company_introduction", companyIntroduction);
        }
        if (!StringUtils.isEmpty(scopeOfBusiness)) {
            wrapper.eq("scope_of_business", scopeOfBusiness);
        }
        if (!StringUtils.isEmpty(companyAddress)) {
            wrapper.eq("company_address", companyAddress);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(portraiture)) {
            wrapper.eq("portraiture", portraiture);
        }
        if (!StringUtils.isEmpty(postbox)) {
            wrapper.eq("postbox", postbox);
        }
        if (!StringUtils.isEmpty(addressUrl)) {
            wrapper.eq("address_url", addressUrl);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        wrapper.eq("delete_flag", 0);
        List<PersonalHomepageInfo> list = personalHomepageInfoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = personalHomepageInfoMapper.selectCount(wrapper);
        return new Page<PersonalHomepageInfo>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String id) {
        String[] ids = String.valueOf(id).split(",");
        for (String tt : ids) {
            PersonalHomepageInfo tmp = personalHomepageInfoMapper.selectById(Long.parseLong(tt));
            if (tmp != null) {
                tmp.setDeleteFlag(1l);
                tmp.setGmtUpdate(new Date());
                personalHomepageInfoMapper.updateById(tmp);
            }
        }
        return true;
    }

    @Override
    public Boolean update(PersonalHomepageInfo personalHomepageInfo) throws ServiceException {
        Date now = new Date();
        personalHomepageInfo.setGmtUpdate(now);
        return personalHomepageInfoMapper.updateById(personalHomepageInfo) > 0;
    }

    @Override
    public PersonalHomepageInfo get(Long id) throws ServiceException {
        return personalHomepageInfoMapper.selectById(id);
    }

    @Override
    public String export(Long commonId, String companyName, String companyIntroduction, String scopeOfBusiness, String companyAddress, String phone, String portraiture, String postbox, String addressUrl, Date gmtCreate, Date gmtUpdate, Long deleteFlag, Integer page, Integer limit) throws ServiceException {
        Wrapper<PersonalHomepageInfo> wrapper = new EntityWrapper<PersonalHomepageInfo>();
        if (!StringUtils.isEmpty(commonId)) {
            wrapper.eq("common_id", commonId);
        }
        if (!StringUtils.isEmpty(companyName)) {
            wrapper.eq("company_name", companyName);
        }
        if (!StringUtils.isEmpty(companyIntroduction)) {
            wrapper.eq("company_introduction", companyIntroduction);
        }
        if (!StringUtils.isEmpty(scopeOfBusiness)) {
            wrapper.eq("scope_of_business", scopeOfBusiness);
        }
        if (!StringUtils.isEmpty(companyAddress)) {
            wrapper.eq("company_address", companyAddress);
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.eq("phone", phone);
        }
        if (!StringUtils.isEmpty(portraiture)) {
            wrapper.eq("portraiture", portraiture);
        }
        if (!StringUtils.isEmpty(postbox)) {
            wrapper.eq("postbox", postbox);
        }
        if (!StringUtils.isEmpty(addressUrl)) {
            wrapper.eq("address_url", addressUrl);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            wrapper.eq("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtUpdate)) {
            wrapper.eq("gmt_update", gmtUpdate);
        }
        if (!StringUtils.isEmpty(deleteFlag)) {
            wrapper.eq("delete_flag", deleteFlag);
        }
        List<PersonalHomepageInfo> list = personalHomepageInfoMapper.selectList(wrapper);
        ExcelUtil<PersonalHomepageInfo> util = new ExcelUtil<PersonalHomepageInfo>(PersonalHomepageInfo.class);
        return util.exportExcel(list, "操作日志");
    }
}
