package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.manage.entity.BaseProject;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.ProjectRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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.RestController;

@Api(value = "项目controller", tags = {"项目相关接口"})
@RestController
@RequestMapping("/demay/ssm/pro")
@Slf4j
public class SsmProjectController {

    @Autowired
    private SsmProjectService ssmProjectService;

    @ApiOperation(value = "分页查询项目 app端-项目-分页查询项目", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody ProjectRequest projectRequest) {
        PageResponse<BaseProject> pageResponse = new PageResponse<>();
        PageInfo<BaseProject> pageInfo = new PageInfo<>();
//        projectRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmProjectService.selectPage(projectRequest);
        } catch (Exception e) {
            log.error("SsmProjectController.page", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "项目明细查询  门店端-项目-项目明细查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/one")
    public GenericResponse one(@RequestBody ProjectRequest projectRequest) {
        GenericResponse<BaseProject> genericResponse = new GenericResponse<>();
//        projectRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        BaseProject baseProject = new BaseProject();
        try {
            baseProject = ssmProjectService.one(projectRequest);
        } catch (Exception e) {
            log.error("SsmProjectController.one", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(baseProject);
        return genericResponse;
    }

    @ApiOperation(value = "项目添加  门店端-项目-项目添加", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody ProjectRequest projectRequest) {
        GenericResponse<BaseProject> genericResponse = new GenericResponse<>();
        if (StringUtils.isBlank(projectRequest.getProjectNum())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"项目编码");
            return genericResponse;
        }
        if (StringUtils.isBlank(projectRequest.getProjectName())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "项目名称");
            return genericResponse;
        }
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String phone = SessionHelp.getLoginAccount(session);
//        projectRequest.setStoreId(storeId);
//        projectRequest.setStoreName(storeName);
        int i = 0;
        try {
            i = ssmProjectService.addProject(projectRequest, projectRequest.getStaffPhone());
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmProjectController.add", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "项目修改  门店端-项目-项目修改", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody ProjectRequest projectRequest) {
        GenericResponse<BaseProject> genericResponse = new GenericResponse<>();

//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        String phone = SessionHelp.getLoginAccount(session);
//        projectRequest.setStoreId(storeId);
//        projectRequest.setStoreName(storeName);
        int i = 0;
        try {
            i = ssmProjectService.updateProject(projectRequest, projectRequest.getStaffPhone());
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmProjectController.update", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "项目删除  门店端-项目-项目删除", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody ProjectRequest projectRequest) {
        GenericResponse<BaseProject> genericResponse = new GenericResponse<>();
//        projectRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmProjectService.deleteProject(projectRequest);
        } catch (SmsException e){
            log.error("SsmProjectController.delete", e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmProjectController.delete", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "项目状态修改  门店端-项目-项目状态修改", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/updateStates")
    public GenericResponse updateStates(@RequestBody ProjectRequest projectRequest) {
        GenericResponse<BaseProject> genericResponse = new GenericResponse<>();
//        projectRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmProjectService.updateStates(projectRequest);
        }catch (SmsException e) {
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmProjectController.updateStates", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "项目设置业绩提成   门店端-项目-项目设置业绩提成", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/setAchiComm")
    public GenericResponse setAchiComm(@RequestBody ProjectRequest projectRequest) {
        GenericResponse<BaseProject> genericResponse = new GenericResponse<>();
//        projectRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmProjectService.setAchiComm(projectRequest);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmProjectController.setAchiComm", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
