package com.mvc.service.taizhang;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TCustomrMapper;
import com.mvc.entity.TCustomr;
import com.mvc.entity.TCustomrExample;
import com.mvc.model.TCustomrModel;
import com.mvc.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//客户登录Service
@Service
@Transactional
public class TCustomrService {

    @Autowired
    private TCustomrMapper tCustomrMapper;

    public Map<String,Object> findAllTCustomr(Integer page, Integer rows) {
        Map<String, Object> resMap=new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        List<TCustomr> TCustomrs = tCustomrMapper.selectByExample(null);
        List<TCustomrModel> resList=new ArrayList<>();
        for(TCustomr TCustomr:TCustomrs){
            TCustomrModel model = new TCustomrModel();
            BeanUtils.copyProperties(TCustomr,model);
            resList.add(model);
        }
        resMap.put("total",pageObject.getTotal());
        resMap.put("list",resList);
        return resMap;
    }

    public TCustomrModel saveTCustomr(TCustomrModel model) {
        model.setCustomerId(StringUtil.getUuid());
        //授权码判重暂时先不处理
        String charAndNumr = StringUtil.getCharAndNumr(6);
        TCustomr TCustomr = new TCustomr();
        model.setAuthId(charAndNumr);
        BeanUtils.copyProperties(model,TCustomr);
        int insert = tCustomrMapper.insert(TCustomr);
        model.setId(TCustomr.getId());
        return model;
    }

    public TCustomrModel findTCustomrByCustomId(String  customerId) {
        TCustomrExample example=new TCustomrExample();
        TCustomrExample.Criteria criteria = example.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        List<TCustomr> tCustomrs = tCustomrMapper.selectByExample(example);
        if(tCustomrs.size()>0){
            TCustomrModel resModel= new TCustomrModel();
            BeanUtils.copyProperties(tCustomrs.get(0),resModel);
            return resModel;
        }else{
            return null;
        }

    }

    public TCustomrModel updateTCustomr(TCustomrModel model) {
        TCustomr TCustomr = tCustomrMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,TCustomr);
        int i = tCustomrMapper.updateByPrimaryKey(TCustomr);
        return model;
    }

    public int deleteTCustomr(int id) {
        int i = tCustomrMapper.deleteByPrimaryKey(id);
        return i;
    }



}
