package com.hwrj.cloud.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.mapper.UmsCompanyShareMapper;
import com.hwrj.cloud.admin.mapper.UmsMemberMapper;
import com.hwrj.cloud.admin.model.UmsAdmin;
import com.hwrj.cloud.admin.model.UmsCompanyShare;
import com.hwrj.cloud.admin.model.UmsCompanyShareExample;
import com.hwrj.cloud.admin.model.UmsMember;
import com.hwrj.cloud.admin.service.UmsCompanyShareService;
import com.hwrj.cloud.admin.util.CurrUserUtil;
import com.hwrj.cloud.common.util.ShareCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UmsCompanyShareServiceImpl implements UmsCompanyShareService {

    @Autowired
    private UmsCompanyShareMapper umsCompanyShareMapper;

    @Override
    public String addShare(Long compId, String compName) {
        if (compId == null){
            return null;
        }
        UmsAdmin umsAdmin = CurrUserUtil.getUmsAdmin();
        if (umsAdmin == null){
            return null;
        }

        //封装实体插入实体
        String code = ShareCodeUtil.toSerialCode(compId);
        UmsCompanyShare umsCompanyShare = new UmsCompanyShare();
        umsCompanyShare.setCompId(compId);
        umsCompanyShare.setCompName(compName);
        umsCompanyShare.setCreateId(umsAdmin.getId());
        umsCompanyShare.setCreateName(umsAdmin.getUsername());
        umsCompanyShare.setCreateTime(new Date());
        umsCompanyShare.setShareCode(code);
        umsCompanyShare.setStatus(0);
        umsCompanyShare.setUpdateTime(new Date());
        //插入实体表
        int insert = umsCompanyShareMapper.insert(umsCompanyShare);
        return code;
    }


    @Autowired
    private UmsMemberMapper umsMemberMapper;

    @Override
    public List<UmsCompanyShare> shareList(Long compId,int status, int pageNo, int pageSize) {
        if (compId == null){
            return null;
        }
        UmsCompanyShareExample umsCompanyShareExample = new UmsCompanyShareExample();
        UmsCompanyShareExample.Criteria criteria = umsCompanyShareExample.createCriteria();
        criteria.andCompIdEqualTo(compId);
        if (status == 0||status == 1){
            criteria.andStatusEqualTo(status);
        }
        criteria.andStatusNotEqualTo(2);
        PageHelper.startPage(pageNo,pageSize);
        List<UmsCompanyShare> shares = umsCompanyShareMapper.selectByExample(umsCompanyShareExample);
        for (UmsCompanyShare share : shares) {
            Long memberId = share.getMemberId();
            if(!StringUtils.isEmpty(memberId)){
                UmsMember umsMember = umsMemberMapper.selectByPrimaryKey(memberId);
                share.setMember(umsMember.getNickname());
            }
        }
        return shares;
    }

    @Override
    public UmsCompanyShare shareDetail(Long id) {
        if (id == null){
            return null;
        }
        return umsCompanyShareMapper.selectByPrimaryKey(id);
    }

    @Override
    public void shareUpdate(Long id, int status) {
        if (id  == null){
            throw  new RuntimeException("非法操作");
        }
        UmsCompanyShare us = new UmsCompanyShare();
        us.setId(id);
        us.setStatus(status);
        int i = umsCompanyShareMapper.updateByPrimaryKeySelective(us);
    }
}
