package com.ssm.manage.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.BaseDao;
import com.ssm.manage.dao.CooperateListDao;
import com.ssm.manage.dao.CooperateProjectDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.CooperateListRequest;
import com.ssm.manage.query.CooperateProjectRequst;
import com.ssm.manage.query.ProjectRequest;
import com.ssm.manage.response.*;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.util.RedisClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;


/**
 * (CouponShare)表服务实现类
 *
 * @author makejava
 * @since 2022-06-11 15:00:06
 */
@Service
public class CooperateProjectServiceImpl extends BaseServiceImpl<CooperateProject> implements CooperateProjectService {

    @Autowired
    private CooperateProjectDao dao;

    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private CooperateListDao cooperateListDao;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    private static String staffKey = "staffKey";
    @Autowired
    private RedisClient<StaffResponse> staffRedisClient;

    @Override
    public BaseDao<CooperateProject> getDao() {
        return dao;
    }





    @Override
    public GenericResponse insert(CooperateProjectRequst cooperateProjectRequst, HttpSession session) {
        CooperateProject cooperateProject = new CooperateProject();
        BeanUtils.copyProperties(cooperateProjectRequst,cooperateProject);
        cooperateProject.setId(getId());
        StaffResponse staff = getStaffKey(session);
        cooperateProject.setInputBy(staff.getStaffName());
        cooperateProject.setInputDate(new Date());
        cooperateProject.setStoreId(SessionHelp.getStoreId(session));
        dao.insert(cooperateProject);
        return getCooperateProjectGenericResponse();
    }

    @Override
    public GenericResponse delete(Long id) {
        dao.deleteByPrimaryKey(id);
        return getCooperateProjectGenericResponse();
    }

    @Override
    public PageResponse page(CooperateProjectRequst cooperateProject) {
        CooperateProject cooperateProject1 = new CooperateProject();
        BeanUtils.copyProperties(cooperateProject,cooperateProject1);
        PageHelper.startPage(cooperateProject.getPageNum(),cooperateProject.getPageSize());
        List<CooperateProject> projects = dao.select(cooperateProject1);
        PageInfo<CooperateProject> projectPageInfo = new PageInfo<>(projects);
        PageResponse<CooperateProject> response = new PageResponse<>();
        response.setCodeAndMsg(ResponseCode.OK);

        response.setPageInfo(projectPageInfo);
        return response;
    }

    @Override
    public GenericResponse update(CooperateProjectRequst cooperateListRequest, HttpSession session) {
        CooperateProject cooperateProject = new CooperateProject();
        BeanUtils.copyProperties(cooperateListRequest,cooperateProject);
        StaffResponse staffResponse = getStaffKey(session);
        cooperateProject.setUpdateBy(staffResponse.getStaffName());
        cooperateProject.setUpdateDate(new Date());
//        cooperateProject.setId(getId());
        dao.updateByPrimaryKey(cooperateProject);
        return getCooperateProjectGenericResponse();
    }

    private GenericResponse<CooperateProject> getCooperateProjectGenericResponse() {
        GenericResponse<CooperateProject> genericResponse = new GenericResponse<>();
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    public Long getId(){
        return new SnowflakeUtil(new Random().nextInt(1023)).nextId();
    }

    public StaffResponse getStaffKey(HttpSession session){
        String phone = SessionHelp.getLoginAccount(session);
        return staffRedisClient.get(staffKey + phone);
    }
}