package com.mvc.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TRgeAuthMapper;
import com.mvc.entity.TAffiliatedunit;
import com.mvc.entity.TRgeAuth;
import com.mvc.entity.TRgeAuthExample;
import com.mvc.model.TAffiliatedunitModel;
import com.mvc.model.TRgeAuthModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class TRgeAuthService {
    @Resource
    private TRgeAuthMapper tRgeAuthMapper;

    @Resource
    private TAffiliatedunitService tAffiliatedunitService;

    public Map<String, Object> findAllTRgeAuth(TRgeAuthModel queryModel) {
        TRgeAuthExample example = new TRgeAuthExample();
        TRgeAuthExample.Criteria criteria = example.createCriteria();

        boolean isNull = false;
        if (StringUtils.isNotEmpty(queryModel.getUnitName())) {
            List<TAffiliatedunit> unitList = tAffiliatedunitService.findAllTAffiliatedunitByUnitName(queryModel.getUnitName());
            if (unitList != null && unitList.size() > 0) {
                List<String> values = new ArrayList<>();
                for (TAffiliatedunit unit : unitList) {
                    values.add(unit.getUnitCode());
                }
                criteria.andUnitCodeIn(values);
            } else {
                isNull = true;        //单位名称未匹配到数据
            }
        }

        Map<String, Object> resMap = new HashMap<>();
        List<TRgeAuthModel> resList = new ArrayList<>();

        if(isNull){
            resMap.put("total", 0);
            resMap.put("list", resList);
            return resMap;
        }
        example.setOrderByClause("ID desc");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(),queryModel.getRows());
        List<TRgeAuth> TRgeAuths = tRgeAuthMapper.selectByExample(example);

        for (TRgeAuth TRgeAuth : TRgeAuths) {
            TRgeAuthModel model = new TRgeAuthModel();
            BeanUtils.copyProperties(TRgeAuth, model);

            TAffiliatedunitModel unitModel = tAffiliatedunitService.findTAffiliatedunit(model.getUnitCode());
            if (unitModel != null) {
                model.setUnitName(unitModel.getUnitName());
            }

            if(StringUtils.isNotEmpty(model.getCreateUnitCode())){
                TAffiliatedunitModel createUnitModel = tAffiliatedunitService.findTAffiliatedunit(model.getCreateUnitCode());
                model.setCreateUnitName(createUnitModel==null?"":createUnitModel.getUnitName());
            }
            resList.add(model);
        }

        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public TRgeAuthModel saveTRgeAuth(TRgeAuthModel model) {
        model.setAuthCode(StringUtil.getUuid2());
        model.setAuthUseNum(0);
        model.setCreateTime(new Date());
        TRgeAuth TRgeAuth = new TRgeAuth();
        BeanUtils.copyProperties(model, TRgeAuth);
        int insert = tRgeAuthMapper.insert(TRgeAuth);
        model.setId(TRgeAuth.getId());
        return model;
    }

    public TRgeAuthModel updateTRgeAuth(TRgeAuthModel model) {
        TRgeAuth TRgeAuth = tRgeAuthMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TRgeAuth);
        int i = tRgeAuthMapper.updateByPrimaryKey(TRgeAuth);
        return model;
    }

    public int deleteTRgeAuth(int id) {
        int i = tRgeAuthMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据授权码获取对象
     * @param authCode
     * @return
     */
    public TRgeAuthModel getTRgeAuthModelByAuthCode(String authCode){
        if(StringUtils.isEmpty(authCode)) return null;
        TRgeAuthExample example = new TRgeAuthExample();
        TRgeAuthExample.Criteria criteria = example.createCriteria();
        criteria.andAuthCodeEqualTo(authCode);

        List<TRgeAuth> list = tRgeAuthMapper.selectByExample(example);
        if(list!=null&&list.size()>0){
            TRgeAuthModel model=new TRgeAuthModel();
            BeanUtils.copyProperties(list.get(0),model);
            return model;
        }
        return null;
    }
}
