package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmMemberInitDao;
import com.ssm.manage.entity.MemberInit;
import com.ssm.manage.entity.MemberInitStaff;
import com.ssm.manage.entity.Salesman;
import com.ssm.manage.mapper.MemberInitMapper;
import com.ssm.manage.mapper.MemberInitStaffMapper;
import com.ssm.manage.query.MemberInitRequest;
import com.ssm.manage.service.SsmSalesmanService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Component
public class SsmMemberInitDaoImpl extends BaseDaoImpl<MemberInit> implements SsmMemberInitDao {
    @Autowired
    private MemberInitMapper mapper;

    @Autowired
    private SsmSalesmanService ssmSalesmanService;

    @Autowired
    private MemberInitStaffMapper memberInitStaffMapper;

    @Override
    public MemberInitMapper getMapper() {
        return mapper;
    }

    @Override
    public PageInfo<MemberInit> selectPage(MemberInitRequest memberInitRequest) {
        Example example = new Example(MemberInit.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("storeId",memberInitRequest.getStoreIds());
        criteria.andEqualTo("initState", memberInitRequest.getInitState());
        if (StringUtils.isNotBlank(memberInitRequest.getMemberName())) {
            criteria.andLike("memberName", "%"+memberInitRequest.getMemberName()+"%");
        }
        if (StringUtils.isNotBlank(memberInitRequest.getTradeName())) {
            criteria.andLike("tradeName","%"+ memberInitRequest.getTradeName()+"%");
        }

        if (memberInitRequest.getStandardMoney()!=null) {
            criteria.andEqualTo("standardMoney", memberInitRequest.getStandardMoney());
        }

        if ( null != memberInitRequest.getStartDate() && null != memberInitRequest.getSendDate()){
            criteria.andBetween("modifyDate",memberInitRequest.getStartDate(), memberInitRequest.getSendDate());
        }
        PageHelper.startPage(memberInitRequest.getPageNum(), memberInitRequest.getPageSize(), memberInitRequest.getOrderAndSort());
        List<MemberInit> list = selectByExample(example);
        for (MemberInit memberInit : list) {
            MemberInitStaff memberInitStaff = new MemberInitStaff();
            memberInitStaff.setDocNum(memberInit.getDocNum());
//            List<MemberInitStaff> select = memberInitStaffMapper.select(memberInitStaff);
            Salesman salesman = new Salesman();
            salesman.setOrderNum(memberInit.getDocNum());
//            List<Salesman> select1 = ssmSalesmanService.select(salesman);
//            memberInit.setMemberInitStaffList(select);
//            memberInit.setSalesmanList(select1);
        }
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public int insertBatch(List<MemberInit> list) {
        return mapper.insertBatch(list);
    }

    @Override
    public int updateBatch(List<MemberInit> list) {
        return mapper.updateBatch(list);
    }

    @Override
    public List<MemberInit> selectFKJ(MemberInit memberInit) {
        return mapper.selectFKJ(memberInit);
    }
}
