package com.mvc.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TAffiliatedunitMapper;
import com.mvc.entity.TAffiliatedunit;
import com.mvc.entity.TAffiliatedunitExample;
import com.mvc.model.TAffiliatedunitModel;
import com.mvc.utils.PinyinUtil;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class TAffiliatedunitService {
    @Resource
    private TAffiliatedunitMapper tAffiliatedunitMapper;

    public Map<String, Object> findAllTAffiliatedunit(TAffiliatedunitModel queryModel) {
        Map<String, Object> resMap = new HashMap<>();

        TAffiliatedunitExample example = new TAffiliatedunitExample();

        TAffiliatedunitExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getUnitCode())) {
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }
        if (StringUtils.isNotEmpty(queryModel.getUnitName())) {
            criteria.andUnitNameLike(StringUtil.getLikeFormat(queryModel.getUnitName()));
        }
        if (StringUtils.isNotEmpty(queryModel.getUnitType())) {
            criteria.andUnitTypeEqualTo(queryModel.getUnitType());
        }
        if (StringUtils.isNotEmpty(queryModel.getLeaderName())) {
            criteria.andLeaderNameLike(StringUtil.getLikeFormat(queryModel.getLeaderName()));
        }
        if (StringUtils.isNotEmpty(queryModel.getCreateUnitCode())) {
            criteria.andCreateUnitCodeEqualTo(queryModel.getCreateUnitCode());
        }

        example.setOrderByClause("ID DESC");
        Page<Object> pageObject = null;
        if (queryModel.getPage() != null) {
            pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());
        }

        List<TAffiliatedunitModel> resList = selectByExample(example);

        resMap.put("total", pageObject == null ? resList.size() : pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public List<TAffiliatedunitModel> selectByExample(TAffiliatedunitExample example) {
        List<TAffiliatedunit> TAffiliatedunits = tAffiliatedunitMapper.selectByExample(example);
        List<TAffiliatedunitModel> resList = new ArrayList<>();
        for (TAffiliatedunit TAffiliatedunit : TAffiliatedunits) {
            TAffiliatedunitModel model = new TAffiliatedunitModel();
            BeanUtils.copyProperties(TAffiliatedunit, model);
            if (StringUtils.isNotEmpty(model.getCreateUnitCode())) {
                TAffiliatedunitModel addModel = findTAffiliatedunit(model.getCreateUnitCode());
                model.setCreateUnitName(addModel == null ? "" : addModel.getUnitName());
            }
            resList.add(model);
        }
        return resList;
    }

    /**
     * 根据单位名称模糊查询
     *
     * @param unitName
     * @return
     */
    public List<TAffiliatedunit> findAllTAffiliatedunitByUnitName(String unitName) {
        TAffiliatedunitExample example = new TAffiliatedunitExample();
        TAffiliatedunitExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotEmpty(unitName)) {
            criteria.andUnitNameLike(StringUtil.getLikeFormat(unitName));
        }

        return tAffiliatedunitMapper.selectByExample(example);
    }

    public TAffiliatedunitModel saveTAffiliatedunit(TAffiliatedunitModel model) {
        model.setUnitCode(StringUtil.getUuid());
        model.setCreateUnitFalg(StringUtils.isNotEmpty(model.getCreateUnitFalg()) ? model.getCreateUnitFalg() : "0");
        model.setCreateTime(new Date());
        if (StringUtils.isEmpty(model.getUnitNamePy())) {
            String name = model.getUnitName().replace("（", "").replace("）", "");
            name = name.replace("(", "").replace(")", "");
            //model.setUnitNamePy(ChineseToEn.getAllFirstLetter(name));
            model.setUnitNamePy(PinyinUtil.getPinYinHeadChar(name,true));
        }
        TAffiliatedunit TAffiliatedunit = new TAffiliatedunit();
        BeanUtils.copyProperties(model, TAffiliatedunit);
        int insert = tAffiliatedunitMapper.insert(TAffiliatedunit);
        model.setId(TAffiliatedunit.getId());
        return model;
    }

    public TAffiliatedunitModel updateTAffiliatedunit(TAffiliatedunitModel model) {
        TAffiliatedunit TAffiliatedunit = tAffiliatedunitMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TAffiliatedunit);
        int i = tAffiliatedunitMapper.updateByPrimaryKey(TAffiliatedunit);
        return model;
    }

    public int deleteTAffiliatedunit(int id) {
        int i = tAffiliatedunitMapper.deleteByPrimaryKey(id);
        return i;
    }

    public TAffiliatedunitModel findTAffiliatedunit(String unitCode) {
        if (StringUtils.isEmpty(unitCode)) return null;
        TAffiliatedunitExample example = new TAffiliatedunitExample();
        TAffiliatedunitExample.Criteria criteria = example.createCriteria();
        criteria.andUnitCodeEqualTo(unitCode);

        List<TAffiliatedunit> list = tAffiliatedunitMapper.selectByExample(example);

        if (list != null && list.size() > 0) {
            TAffiliatedunitModel model = new TAffiliatedunitModel();
            BeanUtils.copyProperties(list.get(0), model);
            return model;
        }
        return null;
    }

    public TAffiliatedunit findAffiliatedunitByUnitName(String unitName) {
        TAffiliatedunitExample example = new TAffiliatedunitExample();
        TAffiliatedunitExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotEmpty(unitName)) {
            criteria.andUnitNameEqualTo(unitName);
        }

        List<TAffiliatedunit> list = tAffiliatedunitMapper.selectByExample(example);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    /**
     * 根据createUnitCode获取单位列表(包含createUnitCode是null的情况)
     *
     * @param createUnitCode
     * @return
     */
    public List<TAffiliatedunitModel> findAffiliatedunitByCreateUnitCode(String createUnitCode) {
        List<TAffiliatedunitModel> resList = new ArrayList<>();
        List<TAffiliatedunit> list = tAffiliatedunitMapper.findTAffiliatedunitByCreateUnitCode(createUnitCode);
        for (TAffiliatedunit tAffiliatedunit : list) {
            TAffiliatedunitModel model = new TAffiliatedunitModel();
            BeanUtils.copyProperties(tAffiliatedunit, model);
            resList.add(model);
        }

        return resList;
    }

    /**
     * 根据createUnitCode获取单位列表(绝对匹配)
     *
     * @param createUnitCode
     * @return
     */
    public List<TAffiliatedunitModel> findAffiliatedunitByCreateUnitCode2(String createUnitCode) {
        TAffiliatedunitExample example = new TAffiliatedunitExample();
        example.createCriteria().andCreateUnitCodeEqualTo(createUnitCode);
        return selectByExample(example);
    }

    /**
     * 根据createUnitCode获取单位列表(绝对匹配)
     *
     * @param createUnitCode
     * @return
     */
    public List<TAffiliatedunitModel> findAffiliatedunitByCreateUnitCode2(String createUnitCode, int pageNum, int pageSize) {
        TAffiliatedunitExample example = new TAffiliatedunitExample();
        example.createCriteria().andCreateUnitCodeEqualTo(createUnitCode);

        return selectByExample(example);
    }

    /**
     * 获取全部TAffiliatedunit Map<unitCode,TAffiliatedunit>
     *
     * @return
     */
    public Map<String, TAffiliatedunit> findAllAffiliatedunitMap() {
        Map<String, TAffiliatedunit> map = new HashMap<>();
        List<TAffiliatedunit> affiliatedunits = tAffiliatedunitMapper.selectByExample(null);
        for (TAffiliatedunit affiliatedunit : affiliatedunits) {
            map.put(affiliatedunit.getUnitCode(), affiliatedunit);
        }
        return map;
    }
}
