package com.boman.web.core.service.fxry;

import com.boman.common.core.utils.StringUtils;
import com.boman.common.core.utils.obj.ObjectUtils;
import com.boman.common.core.utils.sql.SqlUtil;
import com.boman.common.core.web.page.PageDomain;
import com.boman.common.core.web.page.TableSupport;
import com.boman.domain.Fxry;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.mapper.FxryMapper;
import com.boman.web.core.utils.AuthUtils;
import com.boman.web.core.utils.IdCardUtils;
import com.boman.web.core.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.google.common.base.Strings.nullToEmpty;

/**
 * @author shiqian
 * @date 2022年01月05日 16:18
 **/
@Service
public class FxryServiceImpl implements FxryService {

    private static final String DEFAULT = "安徽省安庆市潜山市";
    @Resource
    private FxryMapper mapper;

    @Override
    public AjaxResult add(Fxry fxry) {
        String idCard = fxry.getIdCard();
        int count = mapper.countByIdCard(idCard);
        if (count > 0) {
            return AjaxResult.error("此人已经存在，请勿重复添加");
        }

        fxry.setIdCardHide(IdCardUtils.hideIdCard(idCard));
        fxry.setCreateBy(AuthUtils.getLoginUser().getSysUser().getUserName());
        fxry.setUpdateBy(AuthUtils.getLoginUser().getSysUser().getUserName());
        fxry.setAge(IdCardUtils.getAge(idCard));
        fxry.setId(IdUtils.getMaxId("fxry", "id").toString());
        packWzhjdz(fxry);

        int add = mapper.add(fxry);
        return add > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult edit(Fxry fxry) {
        fxry.setIdCardHide(IdCardUtils.hideIdCard(fxry.getIdCard()));
        fxry.setUpdateBy(AuthUtils.getLoginUser().getSysUser().getUserName());
        packWzhjdz(fxry);

        int edit = mapper.edit(fxry);
        return edit > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult delete(String id) {
        int delete = mapper.delete(id, AuthUtils.getLoginUser().getSysUser().getUserName());
        return delete > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public Fxry getById(String id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }

        return mapper.getById(id);
    }

    @Override
    public List<Fxry> list(Fxry fxry) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }

        List<Fxry> list = mapper.list(fxry);
        return list;
    }

    private void packWzhjdz(Fxry fxry){
        if (null == fxry) {
            return;
        }

        fxry.setWzhjdz(DEFAULT + nullToEmpty(fxry.getVillageTowns()) + nullToEmpty(fxry.getVillage()) + nullToEmpty(fxry.getGroup()) + nullToEmpty(fxry.getDetailAddr()));
    }
}
