package com.ssm.manage.dao.impl;

import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmCardChangeOutProjectDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.mapper.CardChangeOutProjectMapper;
import com.ssm.manage.mapper.SalesmanMapper;
import com.ssm.manage.query.CardChangeOutProjectRequest;
import com.ssm.manage.query.StaffRequest;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmCourseAccountService;
import com.ssm.manage.service.SsmCourseService;
import com.ssm.manage.service.SsmSalesmanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class SsmCardChangeOutProjectDaoImpl extends BaseDaoImpl<CardChangeOutProject> implements SsmCardChangeOutProjectDao {
    @Autowired
    private CardChangeOutProjectMapper mapper;

    @Autowired
    private SalesmanMapper salesmanMapper;

    @Autowired
    private SsmSalesmanService ssmSalesmanService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @Autowired
    private SsmCourseService ssmCourseService;

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

    @Override
    public int updateStaff(List<CardChangeOutProjectRequest> cardChangeOutProjectRequestList) throws SmsException {
        return mapper.updateStaff(cardChangeOutProjectRequestList);
    }

    @Override
    public int updateStaffLCNT(List<CardChangeOutProjectRequest> cardChangeOutProjectRequests) throws SmsException {
        int i=0;
        for (CardChangeOutProjectRequest cardChangeOutProjectRequest : cardChangeOutProjectRequests) {
//            List<StaffRequest> staffLists = cardChangeOutProjectRequest.getStaffLists();
//            for (int i = 0; i < staffLists.size(); i++) {
//                cardChangeOutProjectRequest.set
//            }
//            CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(cardChangeOutProjectRequest.getAccountId());
//            Salesman salesman = new Salesman();
//            salesman.setOrderNum(courseAccount.getCourseNum());
//            List<Salesman> salesmen = ssmSalesmanService.select(salesman);
//            String docNum = courseAccount.getCourseNum();
//            String orderId ="";
//            for (Salesman salesman1 : salesmen) {
//                 orderId = salesman1.getOrderId();
//                ssmSalesmanService.delete(salesman1);
//            }
//            if(null != cardChangeOutProjectRequest.getSalesmen()){
//                List<Salesman> salesmens = cardChangeOutProjectRequest.getSalesmen();
//                Long storeId = cardChangeOutProjectRequest.getStoreId();
//                BaseStore stores = new BaseStore();
//                stores.setId(storeId);
//                BaseStore baseStore = ssmBaseStoreService.selectOne(stores);
//                for (Salesman salesmen2 : salesmens) {
//                    Random r = new Random(3);
//                    int i1 = r.nextInt();
//                    salesmen2.setId(salesmen2.getId()+i1);
//                    salesmen2.setStaffType(salesmen2.getType());
//                    salesmen2.setOrderId(orderId);
//                    salesmen2.setOrderNum(docNum);
//                    salesmen2.setOrderType(3);
//                    salesmen2.setOrderDate(new Date());
//                    salesmen2.setState(0);
//                    salesmen2.setMoney(cardChangeOutProjectRequest.getChangeOutMoney());
//                    salesmen2.setProjectId(cardChangeOutProjectRequest.getCourseProjectId());
//                    salesmen2.setStoreNum(baseStore.getStoreNum());
//                    salesmen2.setStoreName(baseStore.getStoreName());
//                    salesmen2.setStoreIdOrder(baseStore.getId());
//                }
//                ssmSalesmanService.addBatch(salesmens);
//            }
            List<AchiCommEntity> entities = cardChangeOutProjectRequest.getEntities();
            CardChangeOutProject cardChangeOutProject = new CardChangeOutProject();
            BeanUtils.copyProperties(cardChangeOutProjectRequest,cardChangeOutProject);
            cardChangeOutProject.setId(cardChangeOutProjectRequest.getId());
            for (int j = 0; j < entities.size(); j++) {
                if(j==0){
                    cardChangeOutProject.setSaleOneStaffId(entities.get(0).getId());
                    cardChangeOutProject.setSaleOneNumber(entities.get(0).getBrandNumber());
                    cardChangeOutProject.setSaleOneStaffName(entities.get(0).getStaffName());
                    cardChangeOutProject.setSaleOneRatio(entities.get(0).getSale_ratio());
                }
                if(j==1){
                    cardChangeOutProject.setSaleTwoStaffId(entities.get(1).getId());
                    cardChangeOutProject.setSaleTwoNumber(entities.get(1).getBrandNumber());
                    cardChangeOutProject.setSaleTwoStaffName(entities.get(1).getStaffName());
                    cardChangeOutProject.setSaleTwoRatio(entities.get(1).getSale_ratio());
                }
                if(j==2){
                    cardChangeOutProject.setSaleThreeStaffId(entities.get(2).getId());
                    cardChangeOutProject.setSaleThreeNumber(entities.get(2).getBrandNumber());
                    cardChangeOutProject.setSaleThreeStaffName(entities.get(2).getStaffName());
                    cardChangeOutProject.setSaleThreeRatio(entities.get(2).getSale_ratio());
                }
                if(j==3){
                    cardChangeOutProject.setSaleFourStaffId(entities.get(3).getId());
                    cardChangeOutProject.setSaleFourNumber(entities.get(3).getBrandNumber());
                    cardChangeOutProject.setSaleFourStaffName(entities.get(3).getStaffName());
                    cardChangeOutProject.setSaleFourRatio(entities.get(3).getSale_ratio());
                }
                if(j==4){
                    cardChangeOutProject.setSaleFiveStaffId(entities.get(4).getId());
                    cardChangeOutProject.setSaleFiveNumber(entities.get(4).getBrandNumber());
                    cardChangeOutProject.setSaleFiveStaffName(entities.get(4).getStaffName());
                    cardChangeOutProject.setSaleFiveRatio(entities.get(4).getSale_ratio());
                }
                if(j==5){
                    cardChangeOutProject.setSaleSixStaffId(entities.get(5).getId());
                    cardChangeOutProject.setSaleSixNumber(entities.get(5).getBrandNumber());
                    cardChangeOutProject.setSaleSixStaffName(entities.get(5).getStaffName());
                    cardChangeOutProject.setSaleSixRatio(entities.get(5).getSale_ratio());
                }
                if(j==6){
                    cardChangeOutProject.setSaleSevenStaffId(entities.get(6).getId());
                    cardChangeOutProject.setSaleSevenNumber(entities.get(6).getBrandNumber());
                    cardChangeOutProject.setSaleSevenStaffName(entities.get(6).getStaffName());
                    cardChangeOutProject.setSaleSevenRatio(entities.get(6).getSale_ratio());
                }
                if(j==7){
                    cardChangeOutProject.setSaleEightStaffId(entities.get(7).getId());
                    cardChangeOutProject.setSaleEightNumber(entities.get(7).getBrandNumber());
                    cardChangeOutProject.setSaleEightStaffName(entities.get(7).getStaffName());
                    cardChangeOutProject.setSaleEightRatio(entities.get(7).getSale_ratio());
                }

            }

            i += mapper.updateById(cardChangeOutProject);
        }
        return i;
    }
}
