package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.entity.StoreConversion;
import com.ssm.manage.query.StoreConversionQuery;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmStoreConversionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(value="员工转店controller",tags={"员工转店相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeConversion")
@Slf4j
public class SsmStoreConversionController {

    @Autowired
    private SsmStoreConversionService ssmStoreConversionService;

    @ApiOperation(value = "添加门店员工转店申请查询", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse page(@RequestBody StoreConversionQuery storeConversion, HttpSession session) {
        PageResponse<StoreConversionQuery> genericResponse = new PageResponse();
        PageInfo<StoreConversionQuery> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmStoreConversionService.selectPage(storeConversion);
        }catch (Exception e) {
            log.error("SsmStoreConversionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setPageInfo(pageInfo);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "添加门店员工转店申请", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody List<StoreConversion> storeConversion, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String phone = SessionHelp.getLoginAccount(session);
        for (StoreConversion conversion : storeConversion) {
            if (null == conversion.getStoreId()){
                genericResponse.setCodeAndMsg(ResponseCode.MSG,"请选择转入门店");
                return genericResponse;
            }
        }
        int i = 0;
        try {
            i = ssmStoreConversionService.add(storeConversion,phone,SessionHelp.getStoreId(session));
        }catch (Exception e) {
            log.error("SsmStoreConversionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加门店员工转店申请修改", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public GenericResponse update(@RequestBody StoreConversion storeConversion, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreConversionService.update(storeConversion,phone);
        }catch (Exception e) {
            log.error("SsmStoreConversionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加门店员工转店申请（状态修改）", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/updateStatu", method = RequestMethod.POST)
    public GenericResponse updateStatu(@RequestBody StoreConversionQuery storeConversion, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmStoreConversionService.updateStatu(storeConversion,phone);
        }catch (SmsException e){
            log.error("SsmStaffLoginController.getSmsCode:", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreConversionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "添加门店员工转店申请（状态修改）-批量", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/updateStatu2", method = RequestMethod.POST)
    public GenericResponse updateStatu2(@RequestBody List<StoreConversion> storeConversions, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        String phone = SessionHelp.getLoginAccount(session);
        List<StoreConversionQuery> list = new ArrayList<>();
        try {
            list = ssmStoreConversionService.update(storeConversions, phone);
        }catch (Exception e) {
            log.error("SsmStoreConversionController.updateStatu2:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (null == list) {
            //genericResponse.setRows(new ArrayList<>());
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.CHECK_ERR);
            //genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            genericResponse.setRows(list);
        }
        return genericResponse;
    }

    @ApiOperation(value = "分页查询转门店员工信息",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping(value = "/selectPage", method = RequestMethod.POST)
    public GenericResponse selectPage(@RequestBody StoreConversionQuery storeConversionQuery){
        GenericResponse<StoreConversionQuery> genericResponse = new GenericResponse<>();
        //PageInfo<StoreConversionQuery> pageInfo = new PageInfo<>();
        List<StoreConversionQuery> list = new ArrayList<>();
        try {
            list = ssmStoreConversionService.queryPage(storeConversionQuery);
        }catch (Exception e){
            log.error("SsmStoreConversionController.selectPage", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "检测水牌号重复-批量", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public GenericResponse check(@RequestBody List<StoreConversion> storeConversions, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        //String phone = SessionHelp.getLoginAccount(session);
        List<StoreConversionQuery> check = new ArrayList<>();
        try {
            check = ssmStoreConversionService.check(storeConversions);
        }catch (Exception e) {
            log.error("SsmStoreConversionController.check:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        List<StoreConversionQuery> collect = check.stream().filter(s -> s.getTag() == 1).collect(Collectors.toList());
        if (collect.size() > 0){
            genericResponse.setCodeAndMsg(ResponseCode.CHECK_ERR);
        }else {
            genericResponse.setCodeAndMsg(ResponseCode.CHECK_OK);
        }
        genericResponse.setRows(check);
        return genericResponse;
    }
}
